
- 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
MongoDB query to count records on the basis of matching criteria
To count records on the basis of matching criteria, use count(). Let us create a collection with documents −
> db.demo205.insertOne( ... { ... ... "id": "101", ... "Name": "", ... "Age": "", ... "isActive": false ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3d8a3003d395bdc21346eb") } > db.demo205.insertOne( ... { ... ... "id": "102", ... "Name": "Chris", ... "Age": "25", ... "isActive": true ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3d8a3003d395bdc21346ec") } > db.demo205.insertOne( ... { ... ... "id": "103", ... "Name": "", ... "Age": "", ... "isActive": false ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3d8a3003d395bdc21346ed") }
Display all documents from a collection with the help of find() method −
> db.demo205.find();
This will produce the following output −
{ "_id" : ObjectId("5e3d8a3003d395bdc21346eb"), "id" : "101", "Name" : "", "Age" : "", "isActive" : false } { "_id" : ObjectId("5e3d8a3003d395bdc21346ec"), "id" : "102", "Name" : "Chris", "Age" : "25", "isActive" : true } { "_id" : ObjectId("5e3d8a3003d395bdc21346ed"), "id" : "103", "Name" : "", "Age" : "", "isActive" : false }
Following is the query to count records on the basis of matching criteria −
> db.demo205.count({ ... Name: "", ... Age: "", ... "isActive": false ... });
This will produce the following output −
2
- Related Articles
- Add a column count in a MySQL query on the basis of last name records?
- MySQL query to ORDER BY records on the basis of modulus result
- MongoDB query to find on the basis of true or false values
- Query MongoDB with length criteria?
- MySQL query to display records on the basis of conditions IS NULL OR !=1;?
- How to use or operator in MongoDB to fetch records on the basis of existence?
- MySQL query to select records from a table on the basis of a particular month number?
- Finding matching records with LIKE in MongoDB?
- MongoDB query to find and return subdocument with criteria?
- MySQL db query to fetch records from comma separate values on the basis of a specific value
- Search records on the basis of date in MySQL?
- MongoDB query to update all documents matching specific IDs
- MySQL query to alphabetize records and count the duplicates?
- Fetch records on the basis of LastName using MySQL IN()
- MongoDB query to insert but limit the total records

Advertisements