Change order of array elements

2,930 views
Skip to first unread message

Achim Domma

unread,
May 2, 2011, 1:15:21 PM5/2/11
to mongodb-user
Hi,

I'm developing a Rails 3 application using Mongoid. As I did not got
much feedback about my problem in the related mailinglist and
Stackoverflow, I thought I should ask here. Just to make sure that I'm
not asking something that makes no sense in Mongodb. ;-)

I have a document, which holds an array of child elements. What's the
best way to change the ordering of those elements?

cheers,
Achim

Keith Branton

unread,
May 2, 2011, 2:02:29 PM5/2/11
to mongod...@googlegroups.com
Rewrite the document. I do this a lot in my application - that's the only way I've managed it.

or perhaps you could $set the entire array in an atomic operation? I haven't tried that but it should work.

I asked for https://jira.mongodb.org/browse/SERVER-2362 to get partial support for this. Many of my sorted arrays simply contain fks, so this simple jira covers those and would help me a lot.

I do also have many cases where I keep ordered lists of sub-documents in an array and I allow users to change the order of these. I'm not aware of any jira requesting this. If you make one, post back an I'll vote on it.

Richard Kreuter

unread,
May 2, 2011, 2:56:08 PM5/2/11
to mongod...@googlegroups.com
As Kieth Branton described, the only approach available right now is to
replace the array portion of the document, either by rewriting the whole
document or with $set.

Regards,
Richard

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

Reply all
Reply to author
Forward
0 new messages