
- 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
Get the count of a specific value in MongoDB quickly
For faster queries, create an index. To get the count, use count(). Let us create a collection with documents −
> db.demo311.ensureIndex({"Name":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo311.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50cd01f8647eb59e562044") } > db.demo311.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50cd05f8647eb59e562045") } > db.demo311.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50cd06f8647eb59e562046") } > db.demo311.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50cd0af8647eb59e562047") } > db.demo311.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50cd0df8647eb59e562048") } > db.demo311.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50cd0ff8647eb59e562049") }
Display all documents from a collection with the help of find() method −
> db.demo311.find();
This will produce the following output −
{ "_id" : ObjectId("5e50cd01f8647eb59e562044"), "Name" : "Chris" } { "_id" : ObjectId("5e50cd05f8647eb59e562045"), "Name" : "David" } { "_id" : ObjectId("5e50cd06f8647eb59e562046"), "Name" : "David" } { "_id" : ObjectId("5e50cd0af8647eb59e562047"), "Name" : "Chris" } { "_id" : ObjectId("5e50cd0df8647eb59e562048"), "Name" : "Bob" } { "_id" : ObjectId("5e50cd0ff8647eb59e562049"), "Name" : "Chris" }
Following is the query to get the count of a specific value −
> db.demo311.find({"Name":"Chris"}).count();
This will produce the following output −
3
- Related Articles
- Get the count of a specific value in MongoDB
- Get count of array elements from a specific field in MongoDB documents?
- How to get the count of a specific value in a column with MySQL?
- Fetch data between two dates and with a specific value in MongoDB. Group and get the sum with count?
- Find value above a specific value in MongoDB documents?
- Get the count of the number of documents in a MongoDB Collection?
- How to get items with a specific value from documents using MongoDB shell?
- MongoDB query to get a specific number of items
- Count distinct value in MongoDB?
- Update only a specific value in a MongoDB document
- Get specific value of cell in MySQL
- MongoDB $unwind to get the count
- How to get values greater than a specific value from an embedded list in MongoDB?
- MongoDB query to get specific list of names from documents where the value of a field is an array
- MongoDB query to get only a specific number of elements

Advertisements