0
0
MongodbHow-ToBeginner · 2 min read

MongoDB Query to Find Documents with Multiple Conditions

Use db.collection.find({ field1: value1, field2: value2 }) to find documents matching multiple conditions with AND logic in MongoDB.
📋

Examples

Input{ age: 25, city: 'New York' }
Output[{ _id: 1, name: 'Alice', age: 25, city: 'New York' }, { _id: 3, name: 'Carol', age: 25, city: 'New York', status: 'inactive' }]
Input{ status: 'active', score: { $gt: 80 } }
Output[{ _id: 2, name: 'Bob', status: 'active', score: 90 }]
Input{ category: 'books', price: { $lte: 20 } }
Output[]
🧠

How to Think About It

To find documents with multiple conditions, think of each condition as a filter that must be true. MongoDB combines these filters with AND logic by default when you list them inside the find query object. So, you just add each condition as a key-value pair inside the query.
📐

Algorithm

1
Get the collection to search in.
2
Create a query object with multiple key-value pairs representing conditions.
3
Use the <code>find</code> method with this query object.
4
Return all documents that match all conditions.
💻

Code

mongodb
db.users.insertMany([
  { _id: 1, name: 'Alice', age: 25, city: 'New York' },
  { _id: 2, name: 'Bob', age: 30, city: 'Chicago', status: 'active', score: 90 },
  { _id: 3, name: 'Carol', age: 25, city: 'New York', status: 'inactive' }
]);

const query = { age: 25, city: 'New York' };
const results = db.users.find(query).toArray();
printjson(results);
Output
[ { "_id": 1, "name": "Alice", "age": 25, "city": "New York" }, { "_id": 3, "name": "Carol", "age": 25, "city": "New York", "status": "inactive" } ]
🔍

Dry Run

Let's trace the query { age: 25, city: 'New York' } through the find operation.

1

Start with all documents

[{_id:1, age:25, city:'New York'}, {_id:2, age:30, city:'Chicago'}, {_id:3, age:25, city:'New York'}]

2

Filter by age: 25

[{_id:1, age:25, city:'New York'}, {_id:3, age:25, city:'New York'}]

3

Filter by city: 'New York'

[{_id:1, age:25, city:'New York'}, {_id:3, age:25, city:'New York'}]

StepDocuments After Filter
1[{_id:1, age:25, city:'New York'}, {_id:2, age:30, city:'Chicago'}, {_id:3, age:25, city:'New York'}]
2[{_id:1, age:25, city:'New York'}, {_id:3, age:25, city:'New York'}]
3[{_id:1, age:25, city:'New York'}, {_id:3, age:25, city:'New York'}]
💡

Why This Works

Step 1: Multiple conditions in one object

MongoDB treats multiple key-value pairs inside the find query as AND conditions, so all must be true for a document to match.

Step 2: Filtering documents

The database checks each document to see if it meets every condition in the query object.

Step 3: Returning matched documents

Only documents that satisfy all conditions are returned in the result set.

🔄

Alternative Approaches

Using $and operator
mongodb
db.users.find({ $and: [ { age: 25 }, { city: 'New York' } ] }).toArray();
Explicitly uses $and operator; useful for complex queries but same result as simple object for AND.
Using $or operator for multiple conditions
mongodb
db.users.find({ $or: [ { age: 25 }, { city: 'New York' } ] }).toArray();
Finds documents matching any condition (OR logic), different from default AND.

Complexity: O(n) time, O(k) space

Time Complexity

The query scans documents in the collection; time grows linearly with the number of documents (n).

Space Complexity

Space depends on the number of matched documents (k) returned; query object size is minimal.

Which Approach is Fastest?

Using a simple query object with multiple conditions is fastest and most readable for AND logic; $and operator is equivalent but more verbose.

ApproachTimeSpaceBest For
Simple query objectO(n)O(k)Most common AND queries, simple and fast
$and operatorO(n)O(k)Complex queries needing explicit AND, same performance
$or operatorO(n)O(k)Queries needing OR logic, different use case
💡
List multiple conditions inside the find query object to combine them with AND logic easily.
⚠️
Using multiple separate find calls instead of combining conditions in one query object.