
- 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 implement OR operator in find()
Let us create a collection with documents −
> db.demo78.insertOne({"Name1":"Chris","Name2":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd99c71bf0181ecc4228f") } > db.demo78.insertOne({"Name1":"Bob","Name2":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd9ac71bf0181ecc42290") } > db.demo78.insertOne({"Name1":"David","Name2":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd9b671bf0181ecc42291") } > db.demo78.insertOne({"Name1":"Jace","Name2":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bd9bf71bf0181ecc42292") }
Display all documents from a collection with the help of find() method −
> db.demo78.find();
This will produce the following output −
{ "_id" : ObjectId("5e2bd99c71bf0181ecc4228f"), "Name1" : "Chris", "Name2" : "Mike" } { "_id" : ObjectId("5e2bd9ac71bf0181ecc42290"), "Name1" : "Bob", "Name2" : "Carol" } { "_id" : ObjectId("5e2bd9b671bf0181ecc42291"), "Name1" : "David", "Name2" : "Sam" } { "_id" : ObjectId("5e2bd9bf71bf0181ecc42292"), "Name1" : "Jace", "Name2" : "John" }
Following is the query to implement OR operator in find() −
> db.demo78.find({$or: [{"Name1": "Bob"}, {Name2: "John"}] });
This will produce the following output −
{ "_id" : ObjectId("5e2bd9ac71bf0181ecc42290"), "Name1" : "Bob", "Name2" : "Carol" } { "_id" : ObjectId("5e2bd9bf71bf0181ecc42292"), "Name1" : "Jace", "Name2" : "John" }
- Related Articles
- How to implement MongoDB $or operator
- How to implement MongoDB $or query?
- MongoDB Query to implement $in in array
- MongoDB query to implement aggregate function
- How to query with nand operator in MongoDB?
- Can I query on a MongoDB index if my query contains the $or operator?
- How to query MongoDB using the $ne operator?
- MongoDB to fetch documents with $or Operator
- Implement a query similar to MySQL Union with MongoDB?
- MongoDB query to implement nor query to fetch documents except a specific document
- Find values with OR operator in MongoDB and format the result.?
- MongoDB Query to combine AND & OR?
- MongoDB query to find on the basis of true or false values
- Select multiple values with MongoDB OR operator
- MongoDB query to find last object in collection?

Advertisements