Size of an embedded Array without transferring it to he client

364 views
Skip to first unread message

Marc Seeger

unread,
Mar 18, 2010, 2:46:59 PM3/18/10
to mongodb-user
Hi,
Is there a way to actually get the size of an Array without
transferring the whole thing to my client and call.size() in my
programming language of choice?
(e.g. like count(), but for embedded arrays)

Thanks,
Marc

Mathias Stearn

unread,
Mar 18, 2010, 4:38:33 PM3/18/10
to mongod...@googlegroups.com
I'd recommend adding a count or size field to your objects and
maintain it with an $inc everytime you $push. That is the most
efficient way to do it.

> --
> 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.
>
>

Marc Seeger

unread,
Mar 18, 2010, 4:52:22 PM3/18/10
to mongodb-user
My problem is that I use addToSet with multiple values (using $each)
and don't really know how many actually get pushed in the array.
Could I use Server-side Code Execution to go through all of my records
and write the size of the arrays to the count field?

Mathias Stearn

unread,
Mar 18, 2010, 4:55:25 PM3/18/10
to mongod...@googlegroups.com
Ahh, ok. No good solution yet. Please file a Jira requesting a way to
update a size field with addToSet. We should be able to do it really
efficiently serverside.

Marc Seeger

unread,
Mar 18, 2010, 5:16:55 PM3/18/10
to mongodb-user
Would a bit of javascript code be able to do that as a "workarround"
at the Moment?

Mathias Stearn

unread,
Mar 18, 2010, 5:19:26 PM3/18/10
to mongod...@googlegroups.com
yes, but db.eval locks the whole server currently and is much slower
than normal queries
Reply all
Reply to author
Forward
0 new messages