get size of array?

835 views
Skip to first unread message

Earthson

unread,
Apr 22, 2012, 9:09:24 AM4/22/12
to mongod...@googlegroups.com
for document {'a':(1,2,3), '_id':Object(xxxx...)}, how could I get the size of 'a'. 
I do not find a way to do this, someone help~ 

--


Perfection is achieved 
not when there is nothing more to add
 but when there is nothing left to take away

Scott Hernandez

unread,
Apr 22, 2012, 9:53:11 AM4/22/12
to mongod...@googlegroups.com
You need to do this at the client/app.

In the mongo javascript shell you can do this: doc.a.length

You may also want to look at the query operators:
http://mongodb.org/display/DOCS/Advanced+Queries

> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mongodb-user?hl=en.

Asoka Sampath Edussooriya

unread,
Apr 22, 2012, 9:58:32 AM4/22/12
to mongod...@googlegroups.com
I am not sure about the exact way of getting the size or length of the array. but this is similar what you asking. 

db.collection.find({a:{$size:3}});

this will match document which has the array size of 3

Thanks!


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.



--
Asoka Sampath Edussooriya

Earthson

unread,
Apr 22, 2012, 10:38:50 AM4/22/12
to mongod...@googlegroups.com
May be I should add an extra field to count the length(with limit operations, not addToSet).
thx~

Scott Hernandez

unread,
Apr 22, 2012, 10:45:17 AM4/22/12
to mongod...@googlegroups.com
If you need to do queries on it with an index, then yes.
Reply all
Reply to author
Forward
0 new messages