$size (Array Length)

Last updated: 2026-10-09

$size (Array Length)

$size matches documents where an array field has exactly the given length. `{ tags: { $size: 3 } }` matches documents with exactly 3 tags. In queries $size accepts only an exact value — you cannot write { $size: { $gt: 3 } }; for range conditions use $expr with the aggregation $size instead.

CategoryArray
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ <field>: { $size: <length> } }

⚙️ Parameters

fieldThe array field to match.
lengthThe exact number of array elements.

Returns:Cursor of matching documents

▶ Example

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

// $size: articles with exactly 3 tags
db.articles.find({ tags: { $size: 3 } })

// At least 3 tags (requires $expr)
db.articles.find({ $expr: { $gte: [{ $size: '$tags' }, 3] } })

Output:

// Returns articles with exactly 3 tags; the second query returns at least 3

📌 Notes

📚 Related Tutorials

❓ FAQ

QCan $size query length greater than a value?
ANot directly. Use $expr: { $gt: [{ $size: '$tags' }, 3] }.
QDoes $size match documents missing the field?
ANo, the field must exist and be an array.
QCan $size: 0 match empty arrays?
AYes, { tags: { $size: 0 } } matches empty arrays; missing fields still do not match.
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%

🙏 帮我们做得更好

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

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