
- 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 aggregation framework match OR is possible?
Let us first create a collection with the document. The query to create a collection with a document is as follows −
> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith","StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ac3a96cea1f28b7aa080d") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Carol", "StudentLastName":"Tayor","StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ac3bc6cea1f28b7aa080e") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ac3ce6cea1f28b7aa080f") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Bob", "StudentLastName":"Taylor","StudentAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ac3e16cea1f28b7aa0810") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Robert", "StudentLastName":"Smith","StudentAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ac3fb6cea1f28b7aa0811") } > db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Mike", "StudentLastName":"Miller","StudentAge":27}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ac4166cea1f28b7aa0812") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.aggregationFrameworkWithOrMatchDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8ac3a96cea1f28b7aa080d"), "StudentFirstName" : "John", "StudentLastName" : "Smith", "StudentAge" : 23 } { "_id" : ObjectId("5c8ac3bc6cea1f28b7aa080e"), "StudentFirstName" : "Carol", "StudentLastName" : "Tayor", "StudentAge" : 24 } { "_id" : ObjectId("5c8ac3ce6cea1f28b7aa080f"), "StudentFirstName" : "David", "StudentLastName" : "Miller", "StudentAge" : 21 } { "_id" : ObjectId("5c8ac3e16cea1f28b7aa0810"), "StudentFirstName" : "Bob", "StudentLastName" : "Taylor", "StudentAge" : 20 } { "_id" : ObjectId("5c8ac3fb6cea1f28b7aa0811"), "StudentFirstName" : "Robert", "StudentLastName" : "Smith", "StudentAge" : 20 } { "_id" : ObjectId("5c8ac4166cea1f28b7aa0812"), "StudentFirstName" : "Mike", "StudentLastName" : "Miller", "StudentAge" : 27 }
Here is the query to aggregate framework match OR. We are considering StudentLastName as Smith or Miller −
> db.aggregationFrameworkWithOrMatchDemo.aggregate({ ... $match: { $or: [{ StudentLastName: 'Smith' }, { StudentLastName: 'Miller' }] }}).pretty();
The following is the output −
{ "_id" : ObjectId("5c8ac3a96cea1f28b7aa080d"), "StudentFirstName" : "John", "StudentLastName" : "Smith", "StudentAge" : 23 } { "_id" : ObjectId("5c8ac3ce6cea1f28b7aa080f"), "StudentFirstName" : "David", "StudentLastName" : "Miller", "StudentAge" : 21 } { "_id" : ObjectId("5c8ac3fb6cea1f28b7aa0811"), "StudentFirstName" : "Robert", "StudentLastName" : "Smith", "StudentAge" : 20 } { "_id" : ObjectId("5c8ac4166cea1f28b7aa0812"), "StudentFirstName" : "Mike", "StudentLastName" : "Miller", "StudentAge" : 27 }
- Related Articles
- Match between fields in MongoDB aggregation framework?
- MongoDB query (aggregation framework) to match a specific field value
- Is it possible to sum two fields in MongoDB using the Aggregation framework?
- Get Absolute value with MongoDB aggregation framework?
- MongoDB aggregation framework with group query example?
- MongoDB aggregation framework to sort by length of array?
- MongoDB query to group several fields using aggregation framework?
- Does aggregation query with $match works in MongoDB?
- Is it possible to rename _id field after MongoDB group aggregation?
- Working with Aggregation to match all the values in MongoDB
- Get the average of an entire field using the aggregation framework in MongoDB?
- Retrieving an embedded object as a document via the aggregation framework in MongoDB?
- How to exclude _id without including other fields using the aggregation framework in MongoDB?
- How to match and group array elements with the max value in MongoDB aggregation?
- MongoDB aggregation to combine or merge fields and then count?

Advertisements