- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 multiple OR conditions on same key?
For this, simply use $or once. Let us create a collection with documents −
> db.demo551.insertOne({"Name":"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e36d39e5f92834d7f05e5") } > db.demo551.insertOne({"Name":"Chris Brown"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e36d89e5f92834d7f05e6") } > db.demo551.insertOne({"Name":"John Doe"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e36de9e5f92834d7f05e7") } > db.demo551.insertOne({"Name":"John Smith"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e36e59e5f92834d7f05e8") } > db.demo551.insertOne({"Name":"Carol"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e36ec9e5f92834d7f05e9") }
Display all documents from a collection with the help of find() method −
> db.demo551.find();
This will produce the following output −
{ "_id" : ObjectId("5e8e36d39e5f92834d7f05e5"), "Name" : "John" } { "_id" : ObjectId("5e8e36d89e5f92834d7f05e6"), "Name" : "Chris Brown" } { "_id" : ObjectId("5e8e36de9e5f92834d7f05e7"), "Name" : "John Doe" } { "_id" : ObjectId("5e8e36e59e5f92834d7f05e8"), "Name" : "John Smith" } { "_id" : ObjectId("5e8e36ec9e5f92834d7f05e9"), "Name" : "Carol" }
Following is the query to implement OR conditions on the same key −
> db.demo551.find({$or:[ ... {"Name" : {$eq : "Carol"}}, ... {Name: {$eq:"John"}} ... ] ... });
This will produce the following output −
{ "_id" : ObjectId("5e8e36d39e5f92834d7f05e5"), "Name" : "John" } { "_id" : ObjectId("5e8e36ec9e5f92834d7f05e9"), "Name" : "Carol" }
- Related Articles
- Implement multiple conditions in MongoDB?
- MongoDB query to get documents with multiple conditions set in $or?
- MongoDB query to $pull / $unset with multiple conditions?
- How to update array with multiple conditions in MongoDB
- While fetching the data as output, how can I use multiple conditions on same column?
- Delete data from a collection in MongoDB using multiple conditions?
- Set multiple conditions in MongoDB and fetch value in a range
- Update quantity in MongoDB based on two conditions?
- Select multiple values with MongoDB OR operator
- JavaScript example to filter an array depending on multiple checkbox conditions.
- Changing the primary key on a MongoDB collection?
- Performing distinct on multiple fields in MongoDB?
- How can we assign FOREIGN KEY constraint on multiple columns?
- Multi-key Indexing on an entire array with MongoDB?
- Which MongoDB query finds same value multiple times in an array?

Advertisements