
- 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 to implement MongoDB $or query?
The syntax is as follows for the $or query in MongoDB −
db.yourCollectionName.find({ $or : [ { "yourFieldName" : "yourValue1" }, {"yourFieldName":"yourValue2"},...........N ] } ).pretty();
To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.orDemo.insertOne({"UserName":"Larry","UserAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9491fd4cf1f7a64fa4df4c") } > db.orDemo.insertOne({"UserName":"David","UserAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9492074cf1f7a64fa4df4d") } > db.orDemo.insertOne({"UserName":"Mike","UserAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5c94920e4cf1f7a64fa4df4e") } > db.orDemo.insertOne({"UserName":"Sam","UserAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c9492144cf1f7a64fa4df4f") } > db.orDemo.insertOne({"UserName":"Carol","UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c94921d4cf1f7a64fa4df50") } > db.orDemo.insertOne({"UserName":"Bob","UserAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c94922c4cf1f7a64fa4df51") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.orDemo.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c9491fd4cf1f7a64fa4df4c"), "UserName" : "Larry", "UserAge" : 23 } { "_id" : ObjectId("5c9492074cf1f7a64fa4df4d"), "UserName" : "David", "UserAge" : 21 } { "_id" : ObjectId("5c94920e4cf1f7a64fa4df4e"), "UserName" : "Mike", "UserAge" : 25 } { "_id" : ObjectId("5c9492144cf1f7a64fa4df4f"), "UserName" : "Sam", "UserAge" : 20 } { "_id" : ObjectId("5c94921d4cf1f7a64fa4df50"), "UserName" : "Carol", "UserAge" : 24 } { "_id" : ObjectId("5c94922c4cf1f7a64fa4df51"), "UserName" : "Bob", "UserAge" : 22 }
Here is the query for $or −
> db.orDemo.find({ $or : [ { "UserName" : "Carol" }, {"UserName":"Larry"} ] } ).pretty();
The following is the output:
{ "_id" : ObjectId("5c9491fd4cf1f7a64fa4df4c"), "UserName" : "Larry", "UserAge" : 23 } { "_id" : ObjectId("5c94921d4cf1f7a64fa4df50"), "UserName" : "Carol", "UserAge" : 24 }
- Related Articles
- MongoDB query to implement OR operator in find()
- How to implement MongoDB $or operator
- MongoDB query to implement aggregate function
- MongoDB Query to implement $in in array
- Implement a query similar to MySQL Union with MongoDB?
- MongoDB query to implement nor query to fetch documents except a specific document
- MongoDB Query to combine AND & OR?
- Invert Result of MongoDB Query (Implement Opposite of $and operation)?
- How to query MongoDB with “like”?
- How to dynamically build MongoDB query?
- MongoDB query which represents not equal to null or empty?
- How to query MongoDB similar to “like” ?
- Implement MySQL query using multiple OR statements. Any optimal alternative?
- How to query for records where field is null or not set in MongoDB?
- MongoDB query to get documents with multiple conditions set in $or?

Advertisements