$size
The $size query operator matches documents where an array field has exactly the specified number of elements. This is useful for filtering documents based on array length.
Parameters
-
field: The array field to check. -
count: The exact number of elements the array must contain.
Example (MongoDB Shell)
The following example demonstrates using the $size operator to find all products that have exactly three tags.
Create sample documents
db.products.insertMany([ { _id: 1, name: "Laptop", tags: ["electronics", "computers", "portable"] }, { _id: 2, name: "Mouse", tags: ["electronics", "accessories"] }, { _id: 3, name: "Desk", tags: ["furniture", "office", "workspace"] }, { _id: 4, name: "Monitor", tags: ["electronics"] } ]);
Query example
db.products.find({ tags: { $size: 3 } });
Output
{ "_id" : 1, "name" : "Laptop", "tags" : [ "electronics", "computers", "portable" ] }
{ "_id" : 3, "name" : "Desk", "tags" : [ "furniture", "office", "workspace" ] }
Code examples
To view a code example for using the $size query operator, choose the tab for the language that you want to use: