w3resource

MongoDB: cursor.size() method

cursor.size

The cursor.size() method is used to return a count of the number of documents that match the db.collection.find() query after applying any cursor.skip() and cursor.limit() methods.

Syntax:

cursor.size()

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.size() method

The following example will return the number of documents that match the query cuisine is equal to American from the collection restaurants.

db.restaurants.find({"cuisine" : "American "}).size();

Output:

> db.restaurants.find({"cuisine" : "American "}).size();
6183

Retrieve the restaurants data from here

Previous: cursor.showDiskLoc() method
Next: cursor.skip() method



Follow us on Facebook and Twitter for latest update.