
- 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 get documents with multiple conditions set in $or?
Let us create a collection with documents −
> db.demo711.insertOne({Name:"John","Marks":75,Age:21,status:"Active"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea85c215d33e20ed1097b7e") } > db.demo711.insertOne({Name:"Chris","Marks":55,Age:22,status:"Active"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea85c2c5d33e20ed1097b7f") } > db.demo711.insertOne({Name:"Bob","Marks":45,Age:20,status:"Inactive"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea85c3e5d33e20ed1097b80") } > db.demo711.insertOne({Name:"David","Marks":85,Age:23,status:"Active"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea85c775d33e20ed1097b81") }
Display all documents from a collection with the help of find() method −
> db.demo711.find();
This will produce the following output −
{ "_id" : ObjectId("5ea85c215d33e20ed1097b7e"), "Name" : "John", "Marks" : 75, "Age" : 21, "status" : "Active" } { "_id" : ObjectId("5ea85c2c5d33e20ed1097b7f"), "Name" : "Chris", "Marks" : 55, "Age" : 22, "status" : "Active" } { "_id" : ObjectId("5ea85c3e5d33e20ed1097b80"), "Name" : "Bob", "Marks" : 45, "Age" : 20, "status" : "Inactive" } { "_id" : ObjectId("5ea85c775d33e20ed1097b81"), "Name" : "David", "Marks" : 85, "Age" : 23, "status" : "Active" }
Following is the MongoDB query with $or −
> db.demo711.find( { $or: [ { Age: {$gte:23}}, {Marks:{$gt:80} }, {status: 'Inactive'} ] }) ;
This will produce the following output −
{ "_id" : ObjectId("5ea85c3e5d33e20ed1097b80"), "Name" : "Bob", "Marks" : 45, "Age" : 20, "status" : "Inactive" } { "_id" : ObjectId("5ea85c775d33e20ed1097b81"), "Name" : "David", "Marks" : 85, "Age" : 23, "status" : "Active" }
- Related Articles
- Fetch multiple documents in MongoDB query with OR condition?
- MongoDB query to $pull / $unset with multiple conditions?
- MongoDB query to add multiple documents
- MongoDB multiple OR conditions on same key?
- MongoDB compound conditions to fetch documents?
- MongoDB query to get distinct FirstName values from documents
- How to update array with multiple conditions in MongoDB
- MongoDB query to get only specific fields in nested array documents?
- Get the maximum mark records from a collection with documents in MongoDB query
- MongoDB query to skip documents
- Set multiple conditions in MongoDB and fetch value in a range
- Implement multiple conditions in MongoDB?
- MongoDB to fetch documents with $or Operator
- MySQL query with OR & AND conditions
- How to update many documents with one query in MongoDB?

Advertisements