
- 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 to fetch documents with $or Operator
The $or operator performs a logical OR operation on an array of two or more expressions. Let us create a collection with documents −
> db.demo674.insertOne({Name:"Chris",Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f33604263e90dac943eb") } > db.demo674.insertOne({Name:"David",Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f33c04263e90dac943ec") } > db.demo674.insertOne({Name:"Bob",Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f34204263e90dac943ed") } > db.demo674.insertOne({Name:"John",Age:24}); { "acknowledged" : true, "insertedId" : ObjectId("5ea3f34804263e90dac943ee") }
Display all documents from a collection with the help of find() method −
> db.demo674.find();
This will produce the following output −
{ "_id" : ObjectId("5ea3f33604263e90dac943eb"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5ea3f33c04263e90dac943ec"), "Name" : "David", "Age" : 23 } { "_id" : ObjectId("5ea3f34204263e90dac943ed"), "Name" : "Bob", "Age" : 21 } { "_id" : ObjectId("5ea3f34804263e90dac943ee"), "Name" : "John", "Age" : 24 }
Following is the query to fetch documents with $or operator −
> db.demo674.find({$or:[{Name:"David"},{Age:21}]});
This will produce the following output −
{ "_id" : ObjectId("5ea3f33604263e90dac943eb"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5ea3f33c04263e90dac943ec"), "Name" : "David", "Age" : 23 } { "_id" : ObjectId("5ea3f34204263e90dac943ed"), "Name" : "Bob", "Age" : 21 }
- Related Articles
- Fetch multiple documents in MongoDB query with OR condition?
- MongoDB aggregation to fetch documents with specific field value?
- MongoDB compound conditions to fetch documents?
- Fetch specific multiple documents in MongoDB
- Fetch all the ids of MongoDB documents?
- How to use or operator in MongoDB to fetch records on the basis of existence?
- MongoDB “$and” operator for subcollection to fetch a document?
- Select multiple values with MongoDB OR operator
- Match ID and fetch documents with $eq in MongoDB in case of array?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- MongoDB query to get documents with multiple conditions set in $or?
- Match MongoDB documents with field value greater than a specific number and fetch them?
- How to implement MongoDB $or operator
- MongoDB query to implement nor query to fetch documents except a specific document
- Fetch specific documents with array values in MongoD

Advertisements