MongoDB count function - cursor method
has average rating
8
out of 10.
Total 10 users rated.
Description
In this page we are going to discuss the usage of count function in mongodb. The count() function returns the number of documents in a specified collection.
Our database name is 'myinfo' and our collection name is 'testtable'. Here, is the collection bellow.
Sample collection "testtable"
MongoDB count() example
If we want to get the number of documents in the collection "testtable", the following mongodb command can be used :
Output of the command

MongoDB count() example with condition
If we want to get the number of documents in the collection "testtable" where the sex is 'Male' , the following mongodb command can be used :
Output of the command

MongoDB count() example with conditional operator
If we want to get the number of documents in the collection "testtable" where the sex is 'Male' and education is 'M.C.A.' , the following mongodb command can be used :
Output of the command

MongoDB count() example with skip()
The count() by default ignores the skip() and limit() parameters.
If we want to get the number of documents after skipping the first document from the collection "testtable" where the sex is 'Male', the following mongodb command can be attempted :
Output of the command

The above statement ignores the skip() parameter.
MongoDB count() example with true parameter
The 'true' parameter with count() can be used to consider the skip and limit values in the calculation.
If we want to get the number of documents after skipping the first document and consider it in the calculation, from the collection "testtable" where the sex is 'Male', the following mongodb command can be used:
Output of the command

MongoDB count() example with dot notation
If we want to get the number of documents in the collection "testtable" where the 'community_name' under the 'extra' is 'MODERN MUSIC', the following mongodb command can be used :
Output of the command


