Do arrays stored in MongoDB keep their order?

Mongodb

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNoah McIlraithView Question on Stackoverflow
Solution 1 - MongodbdmbView Answer on Stackoverflow
Solution 2 - MongodbChris ShainView Answer on Stackoverflow
Solution 3 - MongodbTom SpencerView Answer on Stackoverflow