$in (In Array)

Last updated: 2026-10-09

$in (In Array)

$in selects documents where the field value equals any value in a given array — a convenient shorthand for $or. `{ age: { $in: [18, 21, 30] } }` matches documents where age is 18, 21, or 30. It is more index-friendly than multiple $eq or $or, and has special array-field semantics.

CategoryComparison
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ <field>: { $in: [ <value1>, <value2>, ... ] } }

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $in: match age 18, 21 or 30
db.users.find({ age: { $in: [18, 21, 30] } })

Output:

// Returns documents whose age is any of the listed values

📌 Notes

📚 Related Tutorials

❓ FAQ

QWhich is faster: $in or $or?
A$in is faster. On a single field it performs one index lookup, while $or merges multiple index branches; $in also supports regular expressions.
QIs $in equivalent to $or with multiple $eq?
AFunctionally equivalent, but $in is more concise and performs better with a single index scan. It also accepts $type or regex operands for mixed-form matches.
QHow does $in work on array fields?
AIt matches if any array element equals any value in the $in list, same as $elemMatch with $in but more concise.
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%

🙏 帮我们做得更好

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

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