$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.

CategoryLogical
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $or: [ { <condition1> }, { <condition2> }, ... ] }

⚙️ Parameters

conditions arrayAn 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

📚 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏