$or (Logical OR)
Last updated: 2026-10-09
$or (Logical OR)
$or combines query predicates; a document matches if it satisfies any of them. `{ $or: [ { price: { $lt: 5 } }, { sale: true } ] }`. MongoDB can use an index per clause and merge the results. For many values on one field prefer $in; use $or for any-of logic across different fields.
| Category | Logical |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $or: [ { <condition1> }, { <condition2> }, ... ] }
⚙️ Parameters
| conditions array | An array of query predicates; any one of them suffices. |
|---|
Returns:Cursor of matching documents
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $or: price under 5 or on sale
db.products.find({
$or: [
{ price: { $lt: 5 } },
{ sale: true }
]
})
Output:
// Returns products matching either condition (both count once)
📌 Notes
- Prefer $in over $or for many values of one field
- $or nests with other logical operators; mind the structure with $and
- Each clause may use an index and results merge; full scans are the fallback
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QWhat is the difference between $or and $in?
A$in tests many values on one field; $or combines different fields. Same-field multi-value: use $in.
QDoes $or return duplicates when several conditions match?
ANo. A document is returned at most once even if several conditions match.
QCan $or nest $and?
AYes, each element of $or can itself be $and, $nor, etc.