w3resource
MongoDB Tutorial

MongoDB: cursor.objsLeftInBatch() method

cursor.objsLeftInBatch

The cursor.objsLeftInBatch() method is used to return the number of documents left in the current cursor batch.

The MongoDB instance returns response in batches. To retrieve all the documents from a cursor may require multiple batch responses from the MongoDB instance. When there are no more documents remaining in the current batch, the cursor will retrieve another batch to get more documents until the cursor exhausts.

Syntax:

cursor.objsLeftInBatch()

Sample document in the restaurants collection:


{
  "address": {
     "building": "1007",
     "coord": [ -73.856077, 40.848447 ],
     "street": "Morris Park Ave",
     "zipcode": "10462"
  },
  "borough": "Bronx",
  "cuisine": "Bakery",
  "grades": [
     { "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
     { "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
     { "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
     { "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
     { "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
  ],
  "name": "Morris Park Bake Shop",
  "restaurant_id": "30075445"
}

Example: MongoDB: cursor.objsLeftInBatch() method

The following example will return the number for documents left to return in the current cursor batch from the collection restaurants.

db.restaurants.find().objsLeftInBatch();

Output:

> db.restaurants.find().objsLeftInBatch();
101

Retrieve the restaurants data from here

Previous: cursor.next() method
Next: cursor.pretty() method