Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
$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
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:
- Node.js
-
const { MongoClient } = require('mongodb');
async function main() {
const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
const db = client.db('test');
const profiles = db.collection('profiles');
const result = await profiles.aggregate([
{
$project: {
item: 1,
"numberOfTeams": { $size: "$teams" }
}
}
]).toArray();
console.log(result);
await client.close();
}
main();
- Python
-
from pymongo import MongoClient
def main():
client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
db = client['test']
profiles = db.profiles
result = list(profiles.aggregate([
{
'$project': {
'item': 1,
'numberOfTeams': { '$size': '$teams' }
}
}
]))
print(result)
client.close()
if __name__ == "__main__":
main()