w3resource
MongoDB Tutorial

MongoDB: cursor.count() method

cursor.count

The cursor.count() method is used to return the total number of documents in a cursor. Append the count() method to a find() query to return the number of matching documents.

Syntax:

cursor.count()

Parameters:

Name Description Required /
Optional
Type
applySkipLimit Specifies whether to consider the effects of the cursor.skip() and cursor.limit() methods in the count. By default, the count() method ignores the effects of the cursor.skip() and cursor.limit(). Set applySkipLimit to true to consider the effect of these methods. Optional boolean

Return

When performing a count, MongoDB can return the count using only the index if:

  • the query can use an index
  • the query only contains conditions on the keys of the index, and
  • the query predicates access a single contiguous range of index keys.

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: Count all document from the collection

The following example counts the number of the documents in the restaurants collection.

db.restaurants.count();

Output:

> db.restaurants.count();
25359

Example: Count all Documents that Match a Query

The following example counts the number of the documents in the restaurants collection with the field matching the cuisine is Bakery:

db.restaurants.find({"cuisine" : "Bakery"}).count();

Output:

> db.restaurants.find({"cuisine" : "Bakery"}).count();
691

Example: Count all Documents that Match a Query using more than on criteria

The following example counts the number of the documents in the collection restaurants filterring with the field cuisine is equal to Bakery and zipcode is 10462:

db.restaurants.find( { "cuisine": "Bakery", "address.zipcode": "10462" } ).count();

Output:

> db.restaurants.find( { "cuisine": "Bakery", "address.zipcode": "10462" } ).count();
7

Example: Limit Documents in Count

The following example counts the number of the documents in the collection restaurants filterring with the field cuisine is equal to Bakery and zipcode is 10462 taking into accountthe effect of thelimit(5):

db.restaurants.find( { "cuisine": "Bakery", "address.zipcode": "10462" } ).limit(5).count(true);

Output:

> db.restaurants.find( { "cuisine": "Bakery", "address.zipcode": "10462" } ).limit(5).count(true);
5

Retrieve the restaurants data from here

Previous: cursor.batchSize() method
Next: cursor.explain() method



New Content: Composer: Dependency manager for PHP, R Programming


bedava porno