
- 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
Does aggregation query with $match works in MongoDB?
Yes, it works in MongoDB. Let us create a collection with documents −
> db.demo358.insertOne({"ClientId":101,"ClientName":"Chris","ClientAge":34}); { "acknowledged" : true, "insertedId" : ObjectId("5e5692c0f8647eb59e5620cb") } > db.demo358.insertOne({"ClientId":102,"ClientName":"David","ClientAge":32}); { "acknowledged" : true, "insertedId" : ObjectId("5e5692caf8647eb59e5620cc") } > db.demo358.insertOne({"ClientId":103,"ClientName":"David","ClientAge":35}); { "acknowledged" : true, "insertedId" : ObjectId("5e5692d2f8647eb59e5620cd") } > db.demo358.insertOne({"ClientId":104,"ClientName":"Bob","ClientAge":31}); { "acknowledged" : true, "insertedId" : ObjectId("5e5692ddf8647eb59e5620ce") } > db.demo358.insertOne({"ClientId":105,"ClientName":"Chris","ClientAge":36}); { "acknowledged" : true, "insertedId" : ObjectId("5e5692e7f8647eb59e5620cf") }
Display all documents from a collection with the help of find() method −
> db.demo358.find();
This will produce the following output −
{ "_id" : ObjectId("5e5692c0f8647eb59e5620cb"), "ClientId" : 101, "ClientName" : "Chris", "ClientAge" : 34 } { "_id" : ObjectId("5e5692caf8647eb59e5620cc"), "ClientId" : 102, "ClientName" : "David", "ClientAge" : 32 } { "_id" : ObjectId("5e5692d2f8647eb59e5620cd"), "ClientId" : 103, "ClientName" : "David", "ClientAge" : 35 } { "_id" : ObjectId("5e5692ddf8647eb59e5620ce"), "ClientId" : 104, "ClientName" : "Bob", "ClientAge" : 31 } { "_id" : ObjectId("5e5692e7f8647eb59e5620cf"), "ClientId" : 105, "ClientName" : "Chris", "ClientAge" : 36 }
Here is the how to work with $match in aggregate −
> db.demo358.aggregate( ... [ { $match : { "ClientName" : "David" }}, ... { "$count": "ClientName" } ... ] ... );
This will produce the following output −
{ "ClientName" : 2 }
- Related Articles
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB query to replace value with aggregation?
- MongoDB aggregation framework with group query example?
- Working with Aggregation to match all the values in MongoDB
- Match between fields in MongoDB aggregation framework?
- MongoDB aggregation framework match OR is possible?
- Sort and Group in one MongoDB aggregation query?
- How to match and group array elements with the max value in MongoDB aggregation?
- MongoDB query for exact match
- MongoDB query to get average in aggregation of array element?
- MongoDB aggregation with multiple keys
- MongoDB query to group several fields using aggregation framework?
- Perform min/max with MongoDB aggregation
- MongoDB aggregation with equality inside array?
- How to match date with MongoDB $match?

Advertisements