Updated February 28, 2023
Introduction to MongoDB count
MongoDB count is used to counting the no of a document from collections. We have found the number of documents using the count method in MongoDB. If we have used find method to show the data of collections, it will show all the data, but instead of finding if we have used count, it will display the only actual count of documents. The count is significant and useful to find the actual count of documents. It is beneficial in a large document of collections to find the no of documents only instead of displaying all the data.
Syntax with parameters:
Below is the syntax:
collection_name.count ()
collection_name.count (query, options)
Parameters with count query in MongoDB:
- count: <collection or view>: Count the collection or view documents.
- query: <document>: Query used to count of documents as per query specified.
- limit: <integer>: This is optional parameter of count method in MongoDB.
- skip: <integer>: Matching documents will skip before returning the result of a query.
- hint: <hint>: This is optional parameter of count method in MongoDB.
- readConcern: <document>: The default read concern level of MongoDB is local.
- collation: <document>: This is also optional parameter of count method.
Below is the parameter description of the above syntax:
- Collection name: Collection name is used to display the count of documents from the collection. Collection parameter is more important.
- Count: It is used to count the no of the document from collections, we have found the number of documents using the count method.
- Query: We can use a specified query to find the count of documents mentioned in the query. Using the query, we have filtered the results of documents from collections.
- Limit: Limit is an optional parameter of the count method. We can return the maximum number of counts from the collection using limit.
- Skip: Skip is an optional parameter of the count method. We can skip the number of matching documents before returning the result of the count of collection documents.
- Hint: Hint is an optional parameter. This specifies that the index name is either string or either it is documented.
- Read concern: Read concern is an optional parameter. The level is specified that the default level of real concern.
It has the following syntax:
readConcern: {level: <Value>}
- Collation: Collation is used to the operation of count in MongoDB. Collation is an optional parameter of the count method.
How count Command works in MongoDB?
Below is the working of count method in MongoDB.
- MongoDB count is used to counting the no of the document from collections.
- We have found the number of documents using the count method in MongoDB.
- The count is significant and useful to find the actual count of documents.
- It is handy in a large document of collections to find the no of documents only instead of displaying all the data.
- If we have used find method to show the data of collections, it will show all the data.
- But instead of finding, if we have use count, it will display the only actual count of collection.
- We can define read concern levels as local and majority. If we want to a read concern level as the majority, then we need to specify a nonempty query condition.
- The default override read concern level of count method in it is local.
- We can skip the count of collection documents using the skip parameter. Skip is used to skip the count of documents.
- We have also used the index to return the count of the documents. For fast retrieval count of documents, we are using an index with count method.
- Count method is essential and useful to display the count of all or specified documents.
Examples of MongoDB count
Given below are few of the examples:
We are taking the example of the emp_count table to describe the example of the count method in MongoDB. Below is the data description of emp_count table as follows.
Code:
db.emp_count.find ()
Output:
Example #1 – Count all documents from a collection
In the below example, we have count all documents from emp_count table using count method in MongoDB.
Code:
db.emp_count.count()
db.runCommand( {count: 'emp_count' } )
Output:
The count of all the documents in emp_count table is 12. We have to find a count of documents in two ways.
Example #2 – Count documents that match a query
In the below example, we have to count the documents that match query from emp_count table using count method in MongoDB.
Code:
db.runCommand ( {count:'emp_count', query: { emp_salary: { $gt: 30000 } } } )
db.runCommand ( {count:'emp_count', query: { emp_salary: { $gt: 10000 } } } )
Output:
In the above example, the match count of query greater than 30000 emp_salary is 4, and greater than 10000 emp_salary is 11.
Example #3 – Skip documents in the count
In the below example, we are skipping the documents from count using count method in MongoDB.
Code:
db.runCommand ( {count:'emp_count', query: { emp_salary: { $gt: 10000 } }, skip: 5 } )
db.runCommand ( {count:'emp_count', query: { emp_salary: { $gt: 10000 } }, skip: 10 } )
Output:
Example #4 – Count using default read concern
Below is the example of count using default read concern in MongoDB. We have used read concern levels as local to display the result.
Code:
db.runCommand( { count:'emp_count', query: { emp_salary: { $gt: 10000 } }, readConcern: { level: "local" } } )
Output:
Recommended Articles
This is a guide to MongoDB count. Here we discuss the Introduction, how count command works in MongoDB? Along with examples. You may also have a look at the following articles to learn more –