

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- How to implement MongoDB $or operator
- How to implement MongoDB $or query?
- MongoDB query to implement aggregate function
- MongoDB Query to implement $in in array
- How to query with nand operator in MongoDB?
- Can I query on a MongoDB index if my query contains the $or operator?
- MongoDB to fetch documents with $or Operator
- How to query MongoDB using the $ne operator?
- Find values with OR operator in MongoDB and format the result.?
- MongoDB Query to combine AND & OR?
- Implement a query similar to MySQL Union with MongoDB?
- Select multiple values with MongoDB OR operator
- MongoDB query to implement nor query to fetch documents except a specific document
- MongoDB query to find on the basis of true or false values
- MongoDB query to find last object in collection?
Advertisements