HOWTO · MongoDB

How to Query With Multiple Conditions in MongoDB

Learn how MongoDB combines query conditions with $or, $and, and $in, with practical find() examples.

On this page

Use find() with a filter document to query MongoDB using multiple conditions. Fields in the same filter are combined with AND by default; use $or when any listed clause may match, $and when every clause must match, and $in for equality alternatives on one field.

How MongoDB combines query conditions

A query filter describes which documents to return. For example, db.inventory.find({ quantity: { $gt: 10 }, price: { $lt: 20 } }) matches inventory documents whose quantity is greater than 10 and whose price is less than 20. MongoDB implicitly combines these separate field conditions with AND, so an explicit $and is not needed for this case.

The shell examples below assume db.inventory contains these three documents: { item: "notebook", quantity: 30, price: 12 }, { item: "pen", quantity: 80, price: 20 }, and { item: "folder", quantity: 80, price: 8 }. The first query matches the notebook and folder: both quantities are greater than 10 and both prices are below 20. The pen’s price is 20, so it fails the price condition. The same rule applies when you add more fields to the filter: each condition must match. This makes a plain filter the clearest starting point for “all of these conditions” queries. Keep field names and values consistent with the documents in your collection; MongoDB field names are case-sensitive.

Use $or to match any condition

$or selects documents that match at least one clause in its array. For example, db.inventory.find({ $or: [{ quantity: { $lt: 51 } }, { price: 20 }] }) returns a document when its quantity is below 51 or its price equals 20. The predicate { price: 20 } tests equality; it does not mean that the price is below 20. Use an operator such as $lt to express a comparison.

Here is a small local illustration of the any-condition rule:

inventory = [
    {"item": "notebook", "quantity": 30, "price": 12},
    {"item": "pen", "quantity": 80, "price": 20},
    {"item": "folder", "quantity": 80, "price": 8},
]
matched = [
    item["item"]
    for item in inventory
    if item["quantity"] < 51 or item["price"] == 20
]
print(matched)
['notebook', 'pen']

This Python snippet applies the selection logic to a local Python list; it does not connect to MongoDB. Its output lists the notebook (quantity below 51) and pen (price equals 20). The MongoDB shell query above applies the same any-clause rule to the separately described db.inventory sample through find().

Use $and to require every condition

$and selects a document only when every clause in its array matches. For the sample db.inventory collection, db.inventory.find({ $and: [{ quantity: { $gte: 20 } }, { price: { $lt: 15 } }] }) requires both a quantity of at least 20 and a price below 15. It matches the notebook and folder: each has quantity at least 20 and price below 15.

For different fields, the equivalent implicit-AND filter is db.inventory.find({ quantity: { $gte: 20 }, price: { $lt: 15 } }). Prefer this shorter form when each field appears once. Explicit $and is useful when the query needs to combine multiple expressions on the same field, such as db.inventory.find({ $and: [{ price: { $gte: 10 } }, { price: { $lte: 20 } }] }). It also makes a group of conditions explicit when composing filters programmatically.

Do not rely on the order of clauses in $and to protect another clause from an error. Query expressions are conditions to match, not a guaranteed left-to-right short-circuit sequence.

Index and operator restrictions

Indexes may be relevant to query performance, but whether a particular query benefits depends on the collection, its indexes, and the query. Consider the fields and predicates in your filter when evaluating performance for your own workload. This article does not prescribe a particular index or query plan.

Keep each operator’s role clear when combining conditions: $or expresses alternatives, $and requires all clauses, and ordinary fields in one filter already imply AND. This distinction helps avoid adding logical wrappers that do not change the query’s meaning.

Choose $in for equality alternatives on one field

When several alternatives are equality checks on the same field, $in is usually more concise than repeating that field in $or. For example, db.inventory.find({ price: { $in: [12, 20] } }) matches the notebook and pen, whose prices equal 12 and 20.

Use $or when alternatives involve different fields or predicates other than equality, such as a quantity comparison or a price condition. Use a regular multi-field filter when all of its conditions must match. Choosing among these forms comes down to the intended logic: any clause ($or), every clause ($and, often implicit), or any listed equality value for one field ($in).