
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Difference between count() and find().count() in MongoDB?
There is no difference between count() and find().count(). Let us see how both of them works. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.countDemo.insertOne({"UserId":1,"UserName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f9d278d10a061296a3c5d") } > db.countDemo.insertOne({"UserId":2,"UserName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f9d308d10a061296a3c5e") } > db.countDemo.insertOne({"UserId":3,"UserName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f9d3a8d10a061296a3c5f") } > db.countDemo.insertOne({"UserId":4,"UserName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f9d428d10a061296a3c60") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.countDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c7f9d278d10a061296a3c5d"), "UserId" : 1, "UserName" : "John" } { "_id" : ObjectId("5c7f9d308d10a061296a3c5e"), "UserId" : 2, "UserName" : "Carol" } { "_id" : ObjectId("5c7f9d3a8d10a061296a3c5f"), "UserId" : 3, "UserName" : "Bob" } { "_id" : ObjectId("5c7f9d428d10a061296a3c60"), "UserId" : 4, "UserName" : "Mike" }
Here is the query for count() that counts the number of records −
> db.countDemo.count();
The following is the output −
4
Here is the query for find().count(). The query is as follows −
> db.countDemo.find().count();
The following is the output −
4
- Related Articles
- Difference between count(*) and count(columnName) in MySQL?
- Difference between COUNT and COUNTA
- Find the count of users who logged in between specific dates with MongoDB
- How to count and sum a field between 2 dates in MongoDB?
- Count distinct value in MongoDB?
- MongoDB query to select distinct and count?
- Difference between find() and findOne() methods in MongoDB?
- MongoDB SELECT COUNT GROUP BY?
- MongoDB query to create new field and count set the count of another field in it?
- MongoDB query for ranking / search count?
- MongoDB $unwind to get the count
- Native Querying MongoDB inside array and get the count
- Using count equivalent in MongoDB to find top users with max occurrence
- Get the aggregated result and find the count of repeated values in different MongoDB\ndocuments
- How to count items in array with MongoDB?

Advertisements