Do arrays stored in MongoDB keep their order?
MongodbMongodb Problem Overview
Simple question, do arrays keep their order when stored in MongoDB?
Mongodb Solutions
Solution 1 - Mongodb
yep MongoDB keeps the order of the array.. just like Javascript engines..
Solution 2 - Mongodb
Yes, in fact from a quick google search on the subject, it seems that it's rather difficult to re-order them: http://groups.google.com/group/mongodb-user/browse_thread/thread/1df1654889e664c1
Solution 3 - Mongodb
I realise this is an old question, but the Mongo docs do now specify that all document properties retain their order as they are inserted. This naturally extends to arrays, too.
> ## Document Field Order > > MongoDB preserves the order of the document fields following write operations except for the following cases: > > - The _id field is always the first field in the document. > - Updates that include renaming of field names may result in the reordering of fields in the document. > > Changed in version 2.6: Starting in version 2.6, MongoDB actively attempts to preserve the field order in a document. Before version 2.6, MongoDB did not actively preserve the order of the fields in a document.