MongoDB: sorted arrays

Consider the following MongoDB document:

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

      

The length is nested_object_ids

not limited. Is there an elegant way to keep the array nested_object_ids

sorted after arbitrary values ​​(i.e. ObjectIds) are pressed?

Thank!

+3


source to share


3 answers


Is there an elegant way to keep the array nested_object_ids

sorted after arbitrary values ​​are pressed?

Unfortunately, I don't consider anything "elegant".

The team $push

doesn't work here. The only option is to pull out the entire sub-array in the client and then rewrite it with $set

.



In all fairness, MongoDB has limited functionality when it comes to "arrays of objects". You can upgrade using $push

, $pull

and you can index it in the object field, but more on that.

It is difficult to update a specific sub-object. And the query doesn't return the sub-object, but returns the whole document. You can filter it before returning nested_object_ids

, but you always get the whole set.

The question for you is why nested objects need to be sorted ?

+2


source


Now you can do it with the $ push function to sort the array: https://jira.mongodb.org/browse/SERVER-8008



+2


source


Mongodb's upcoming 2.4 release will allow $ push sorting.

+1


source







All Articles