how to get embedded array's size in mongo? [duplicate]
Possible Duplicate:
Querying internal array size in MongoDB
I have a document which contains an array category
. I just want to know how many items there are in category
. All I could find in the documentation is how to count the number of top level docume开发者_如何学运维nts.
{
"_id": {
"$oid": "4e73a30466ca1a1f56000001"
},
"category": [
"Food",
"Entertainment"
]
}
Add new field to hande category size. It's a usual practice in mongo world.
To find a single example element in a collection callend mycollection:
db.mycollection.find().limit(1)[0];
To get the number of elements in the arry category:
db.mycollection.find().limit(1)[0].category.length;
Or:
var elem = db.mycollection.find().limit(1)[0];
elem.category.length;
精彩评论