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

CategoryArray
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

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

⚙️ Parameters

fieldThe array field to match.
values arrayThe 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

📚 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.
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%

🙏 帮我们做得更好

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

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