3

Consider the following MongoDB document:

SomeObject {
    nested_objects_ids : [
        ObjectId( "1..." ), 
        ObjectId( "2..." ),
        ...
        ObjectId( "N..." )
    ]
}

The length of nested_object_ids is not limited. Is there an elegant way to keep the nested_object_ids array sorted after pushing arbitrary values (i.e. ObjectIds)?

Thank you!

4
  • What are you sorting by? ObjectId itself? If you want them sorted by insertion order that should already happen. Commented Mar 17, 2012 at 14:09
  • +1 for the question because I'd also like to know if there is a good solution. As far as I know, if you want them in a numerical sort order, you'd actually need to write back ($set) the entire array in a sorted form rather than simply using $push : anObjectId Commented Mar 17, 2012 at 14:45
  • @Russel, for example, first an object with _id "45..." is inserted, the next object has _id = "42...". It will be inserted to the end of the array, but I'd like to have it right before the object with id="45...". Commented Mar 17, 2012 at 15:04
  • I think what I'm getting at it why are you using ObjectIds for sorting? They won't sort into any meaningful order. If you're overriding with a custom value for the _id field such as an incrementing int you lose a lot of the benefits of an ObjectId so I would consider revisiting your design. Commented Mar 17, 2012 at 18:50

3 Answers 3

2

Is there an elegant way to keep the nested_object_ids array sorted after pushing arbitrary values?

Unfortunately, there is nothing I would consider "elegant".

The $push command does not work here. Your only option is to pull the entire sub-array into the client and then re-write it with a $set.

Honestly, when it comes to dealing with "arrays of objects", MongoDB has limited functionality. You can update with $push, $pull and you can index on an object field, but that's about it.

It's difficult to update a specific sub-object. And querying doesn't return the sub-object, but instead returns the whole document. You could filter it down to returning nested_object_ids, but you always get the whole set there.

A question for you: why do the nested objects need to be sorted?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for commenting. Consider the situation where ObjectIds kept in this array would come from different non-synchronized sources, so that e.g. ObjectId_n < ObjectId_(n - 1). And you'd like to keep those sorted, because there are hundreds of such entries in the array, and it takes time to sort it every time a new ObjectId is added. I was hoping that there is a way to "tell" MongoDB to keep a particular array in e.g. binary tree :) Maybe someday...
2

You can now do this with the $push to sorted array feature: https://jira.mongodb.org/browse/SERVER-8008

2 Comments

I'd like to add that SERVER-991 documentation provides more information on $sort within $push.
It looks like you can use $orderBy from that feature request... But I am running mongo through meteor (so mongo 2.6) and it doesn't look like I can do this...?
1

The upcoming 2.4 release of Mongodb will allow for sorted $push.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.