
- 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 operator
Evaluate one or more expressions using the $or operator in MongoDB. Following is the syntax −
db.yourCollectionName.find({ $or: [{ "yourFieldName": yourValue1 }, { "yourFieldName": yourValue2} ] } ).pretty();
Let us first create a collection with documents −
> db.orOperatorDemo.insertOne({"StudentNames":["John","Carol","Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6b80a6d78f205348bc61b") } > db.orOperatorDemo.insertOne({"StudentNames":["Robert","Chris","David"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6b8266d78f205348bc61c") } > db.orOperatorDemo.insertOne({"StudentNames":["John"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6b8346d78f205348bc61d") }
Following is the query to display all documents from a collection with the help of find() method −
> db.orOperatorDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd6b80a6d78f205348bc61b"), "StudentNames" : [ "John", "Carol", "Sam" ] } { "_id" : ObjectId("5cd6b8266d78f205348bc61c"), "StudentNames" : [ "Robert", "Chris", "David" ] } { "_id" : ObjectId("5cd6b8346d78f205348bc61d"), "StudentNames" : [ "John" ] }
Here is the query to $or operator syntax −
> db.orOperatorDemo.find({ $or: [{ "StudentNames": "Carol" }, { "StudentNames": "John"} ] } ).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd6b80a6d78f205348bc61b"), "StudentNames" : [ "John", "Carol", "Sam" ] } { "_id" : ObjectId("5cd6b8346d78f205348bc61d"), "StudentNames" : [ "John" ] }
- Related Articles
- MongoDB query to implement OR operator in find()
- How to implement MongoDB $or query?
- MongoDB to fetch documents with $or Operator
- How to implement operator overloading in C#?
- Select multiple values with MongoDB OR operator
- How to implement ternary conditional operator in MySQL?
- How to use or operator in MongoDB to fetch records on the basis of existence?
- How to query MongoDB using the $ne operator?
- How to query with nand operator in MongoDB?
- MongoDB $regex operator i or I for case insensitive search
- Find values with OR operator in MongoDB and format the result.?
- MongoDB query to implement aggregate function
- MongoDB divide aggregation operator?
- How to implement MySQL CASE with OR condition?
- MongoDB Query to implement $in in array

Advertisements