
- 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 compound conditions to fetch documents?
Let us create a collection with documents −
> db.demo714.insertOne({FirstName:"Chris",LastName:"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea9a2da85324c2c98cc4c2b") } > db.demo714.insertOne({FirstName:"Adam",LastName:"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea9a2e585324c2c98cc4c2c") } > db.demo714.insertOne({FirstName:"David",LastName:"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea9a2ed85324c2c98cc4c2d") } > db.demo714.insertOne({FirstName:"John",LastName:"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea9a2f785324c2c98cc4c2e") }
Display all documents from a collection with the help of find() method −
> db.demo714.find();
This will produce the following output −
{ "_id" : ObjectId("5ea9a2da85324c2c98cc4c2b"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5ea9a2e585324c2c98cc4c2c"), "FirstName" : "Adam", "LastName" : "Smith" } { "_id" : ObjectId("5ea9a2ed85324c2c98cc4c2d"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ObjectId("5ea9a2f785324c2c98cc4c2e"), "FirstName" : "John", "LastName" : "Doe" }
Here is the query to compound conditions −
> db.demo714.find({"LastName":{$in:["Miller","Brown"]}});
This will produce the following output −
{ "_id" : ObjectId("5ea9a2da85324c2c98cc4c2b"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5ea9a2ed85324c2c98cc4c2d"), "FirstName" : "David", "LastName" : "Miller" }
- Related Articles
- MongoDB to fetch documents with $or Operator
- Fetch specific multiple documents in MongoDB
- Fetch all the ids of MongoDB documents?
- MongoDB aggregation to fetch documents with specific field value?
- Fetch multiple documents in MongoDB query with OR condition?
- MongoDB query to get documents with multiple conditions set in $or?
- MongoDB query to implement nor query to fetch documents except a specific document
- Set multiple conditions in MongoDB and fetch value in a range
- MongoDB: $nin and $in not working together in $elemMatch to fetch documents having subjects “MongoDB”, but not “Java”
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- Match ID and fetch documents with $eq in MongoDB in case of array?
- Match MongoDB documents with field value greater than a specific number and fetch them?
- MongoDB query to skip documents
- MongoDB query to add multiple documents
- MongoDB query to group duplicate documents

Advertisements