开发者

how to get embedded array's size in mongo? [duplicate]

开发者 https://www.devze.com 2023-04-05 08:14 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Querying internal array size in MongoDB
This question already has answers here: Closed 11 years ago.

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消