$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.
| Category | Comparison |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (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
- An empty array $in: [] matches nothing
- On array fields, matches if any element hits (like $elemMatch)
- Regular expressions are allowed inside $in (but not in $nin)
🏷️ Related Tags
💡 Related Cases
Hands-On: CRUD for a Blog Comment System
$lookup and Associations with Multiple Sets
Mongoose Advanced Patterns: Performance and Flexibility
Document Query Basics: The `find` Method and Projections
📚 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.