$and (Logical AND)
Last updated: 2026-10-09
$and (Logical AND)
$and combines multiple query predicates; a document must satisfy all of them. `{ $and: [ { price: { $gt: 10 } }, { inStock: true } ] }`. Multiple fields in one query document already imply AND; $and makes the logic explicit and is required for several conditions on the same field.
| Category | Logical |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $and: [ { <condition1> }, { <condition2> }, ... ] }
⚙️ Parameters
| conditions array | An array of query predicates; all of them must hold. |
|---|
Returns:Cursor of matching documents
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $and: price above 10 and inStock true
db.products.find({
$and: [
{ price: { $gt: 10 } },
{ inStock: true }
]
})
Output:
// Returns products satisfying both conditions (implicit AND gives the same result)
📌 Notes
- Multiple field predicates already imply AND; use $and only when needed
- Multiple conditions on the same field require $and
- Any failing condition excludes the document; order does not matter
🏷️ Related Tags
💡 Related Cases
Index Types and Advanced Features
Document Query Basics: The `find` Method and Projections
Query Operators: A Complete Guide to the 30+ Operator
📚 Related Tutorials
❓ FAQ
QWhat is the difference between $and and writing multiple fields?
AFunctionally equivalent; $and is for multiple conditions on one field or clearer logical structure.
QCan I write two $gt on the same field?
ADirectly it conflicts (keys are unique); use $and: { $and: [ { score: { $gt: 60 } }, { score: { $gt: 90 } } ] }.
QCan conditions inside $and nest $or?
AYes. Each element of $and can be any predicate, including $or and $nor.