
- 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
How do I use MongoDB to count only collections that match two fields?
To count only collections that match two fields, use count(). Let us create a collection with documents −
> db.demo175.insertOne({"EmployeeName":"Bob","isMarried":"YES"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3840969e4f06af551997e8") } > db.demo175.insertOne({"EmployeeName":"David","isMarried":"NO"}); { "acknowledged" : true, "insertedId" : ObjectId("5e38409e9e4f06af551997e9") } > db.demo175.insertOne({"EmployeeName":"Mike","isMarried":"YES"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3840a79e4f06af551997ea") } > db.demo175.insertOne({"EmployeeName":"Sam","isMarried":"NO"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3840ae9e4f06af551997eb") }
Display all documents from a collection with the help of find() method −
> db.demo175.find();
This will produce the following output −
{ "_id" : ObjectId("5e3840969e4f06af551997e8"), "EmployeeName" : "Bob", "isMarried" : "YES" } { "_id" : ObjectId("5e38409e9e4f06af551997e9"), "EmployeeName" : "David", "isMarried" : "NO" } { "_id" : ObjectId("5e3840a79e4f06af551997ea"), "EmployeeName" : "Mike", "isMarried" : "YES" } { "_id" : ObjectId("5e3840ae9e4f06af551997eb"), "EmployeeName" : "Sam", "isMarried" : "NO" }
Following is the query to count only collections that match two fields −
> db.demo175.count( { "EmployeeName": "Mike", "isMarried":"YES" } );
This will produce the following output −
1
- Related Articles
- How do I work with array fields in MongoDB to match all?
- How do I index “or” in MongoDB for indexing multiple fields?
- How do I search according to fields in inner classes using MongoDB db.coll.find()?
- MongoDB query to update only certain fields?
- Match between fields in MongoDB aggregation framework?
- Get fields from multiple sub-documents that match a condition in MongoDB?
- Update only specific fields in MongoDB?
- MongoDB query for exact match on multiple document fields
- How can I update and increment two fields in one command in MongoDB?
- Count by multiple fields with MongoDB aggregation
- MongoDB query condition to compare two fields?
- How to compare two fields in aggregation filter with MongoDB?
- Match MongoDB documents with fields not containing values in array?
- MongoDB aggregation to combine or merge fields and then count?
- Sort and get only the first two fields in a “$group” operation in MongoDB

Advertisements