$all (Contains All)
Last updated: 2026-10-09
$all (Contains All)
$all matches documents whose array field contains every element of the given array. `{ tags: { $all: ['mongodb', 'database'] } }` requires tags to include both values, in any order. Unlike $in (any match), $all requires all values — ideal for must-have-tag filtering.
| Category | Array |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ <field>: { $all: [ <value1>, <value2>, ... ] } }
⚙️ Parameters
| field | The array field to match. |
|---|---|
| values array | The list of values that must all be present. |
Returns:Cursor of matching documents
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $all: tags include both mongodb and database
db.articles.find({ tags: { $all: ['mongodb', 'database'] } })
// $in for comparison: matches either tag
db.articles.find({ tags: { $in: ['mongodb', 'database'] } })
Output:
// The first query requires both tags; the second matches either one
📌 Notes
- Order does not matter: ['mongodb', 'database'] equals the reverse
- $all is must-contain-all, $in is any-match
- $all: [] matches every document, including those missing the field
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QWhat is the difference between $all and $in?
A$in matches if any value appears; $all requires every listed value to be present.
QDoes $all work on non-array fields?
AOn non-array fields $all behaves like $eq (single-value comparison).
QWhat happens with an empty $all array?
A$all: [] matches all documents, including those missing the field.