$size
The $size operator is used to return the count of items within an array field. This can be used to determine the number of elements in an array stored in a document.
Parameters
-
field: The field path whose array size you want to return.
Example (MongoDB Shell)
This example shows how to use the $size operator to return the count of teams each user is following.
Create sample documents
db.profiles.insertMany([ { _id: 1, name: "John Doe", teams: ["Acme", "Widgets", "Gadgets"] }, { _id: 2, name: "Jane Smith", teams: ["Acme", "Gadgets"] }, { _id: 3, name: "Bob Johnson", teams: ["Acme", "Widgets", "Gadgets"] } ]);
Query example
db.profiles.aggregate([ { $project: { _id: 0, name: 1, "numberOfTeams": { $size: "$teams" } } } ])
Output
[
{ name: 'John Doe', numberOfTeams: 3 },
{ name: 'Jane Smith', numberOfTeams: 2 },
{ name: 'Bob Johnson', numberOfTeams: 3 }
]
Code examples
To view a code example for using the $size command, choose the tab for the language that you want to use: