- 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
Implement multiple conditions in MongoDB?
Let us first create a collection with documents −
> db.multipleConditionDemo.insertOne({"_id":1,"Name":"John"}); { "acknowledged" : true, "insertedId" : 1 } > db.multipleConditionDemo.insertOne({"_id":2,"Name":"Carol"}); { "acknowledged" : true, "insertedId" : 2 } > db.multipleConditionDemo.insertOne({"_id":3,"Name":"Sam"}); { "acknowledged" : true, "insertedId" : 3 } > db.multipleConditionDemo.insertOne({"_id":4,"Name":"David"}); { "acknowledged" : true, "insertedId" : 4 }
Following is the query to display all documents from a collection with the help of find() method −
> db.multipleConditionDemo.find().pretty();
This will produce the following output −
{ "_id" : 1, "Name" : "John" } { "_id" : 2, "Name" : "Carol" } { "_id" : 3, "Name" : "Sam" } { "_id" : 4, "Name" : "David" }
Following is the query to implement multiple conditions in WHERE clause −
> db.multipleConditionDemo.remove({ $or: [ { _id: 2 }, { _id: 3 } ] }); WriteResult({ "nRemoved" : 2 })
Let us check the documents from the above collection once again −
> db.multipleConditionDemo.find().pretty();
This will produce the following output −
{ "_id" : 1, "Name" : "John" } { "_id" : 4, "Name" : "David" }
- Related Articles
- MongoDB multiple OR conditions on same key?
- How to update array with multiple conditions in MongoDB
- MongoDB query to $pull / $unset with multiple conditions?
- Delete data from a collection in MongoDB using multiple conditions?
- Set multiple conditions in MongoDB and fetch value in a range
- MongoDB query to get documents with multiple conditions set in $or?
- Multiple COUNT() for multiple conditions in a single MySQL query?
- MySQL If statement with multiple conditions?
- How to correctly implement conditions in MySQL stored procedure?
- How to work multiple AND conditions in MySQL?
- Implement Text search in MongoDB
- Implement array match in MongoDB?
- 16 Conditions Commonly Mistaken for Multiple Sclerosis
- Is there an operator in MySQL to implement multiple NOT conditions like WHERE id != 5 AND id != 10 AND id != 15?
- MongoDB compound conditions to fetch documents?

Advertisements