How to get the size of an inline array in mongo?

Possible duplicate:
Querying the size of an internal array in MongoDB

I have a document containing a category array. I just want to know how many items are in the category . All I could find in the documentation was counting the number of top-level documents.

 { "_id": { "$oid": "4e73a30466ca1a1f56000001" }, "category": [ "Food", "Entertainment" ] } 
+2
source share
2 answers

Add a new field for the category size. This is a common practice in the mongo world.

+3
source

To find one sample item in the callend mycollection:

 db.mycollection.find().limit(1)[0]; 

To get the number of items in the arry category:

 db.mycollection.find().limit(1)[0].category.length; 

Or:

 var elem = db.mycollection.find().limit(1)[0]; elem.category.length; 
+2
source

All Articles