Simple question, do arrays keep their order when stored in MongoDB?
3 Answers
yep MongoDB keeps the order of the array.. just like Javascript engines..

- 1,066
- 1
- 8
- 10
-
Just like every single language I can think of, actually ;). Which isn't directly related to how a database would store data mind you. – Remon van Vliet Jan 26 '12 at 10:07
-
:) - actually it's not how mongodb is storing the data - it's how it is being parse.. mongodb is using Javacript engine so it's safe to assume that they have the same data type – dmb Feb 28 '12 at 07:37
-
12No that's not why it is the way it is. JavaScript is not used in any part of the MongoDB storage engine. Arrays remain ordered exactly because that's how it's being stored by MongoDB. Array ordering is part of the query specification and drivers, JavaScript or otherwise, follow that spec and present the data as it exists in the database. – Remon van Vliet Feb 28 '12 at 09:24
-
15Again, the *storage engine* doesn't use JavaScript. Obviously there a JS engine in MongoDB for m/r, server-side script execution and other features that are built on top of JavaScript. The fact that MongoDB keeps arrays ordered is due to BSON/JSON spec, not because it uses a JS engine in a completely unrelated part of the database. – Remon van Vliet Mar 21 '12 at 09:53
-
@RemonvanVliet will you provide a link to code or documentation confirming that arrays are kept in order? – Adam Monsen Apr 04 '12 at 05:13
-
@Adam I sincerely doubt it's documented anywhere. It would be akin to documenting integers hold numbers. Arrays as a programming language construct are kept in order until modified by operations and all MongoDB operations that can affect array contents clearly document what the effect on the array is. I'm struggling to figure out why so many people expect arrays to randomly change order. No language exists or database that has an array as a primitive type does so. – Remon van Vliet Apr 04 '12 at 08:28
-
@RemonvanVliet I hear you saying "it should be obvious that arrays have predictable ordering". You probably have more computer science background than I do. Array means different things to me, depending on context. I notice the [php documentation on arrays](http://php.net/array) mentions they are **ordered**, so maybe the MongoDB folks can write something similar about arrays on documents. – Adam Monsen Apr 04 '12 at 21:05
-
@Adam I think the problem might be misuse of the term array more than anything else. For example, code like array["key1"] is pretty common but actually refers to a map structure (or dictionary is some languages call it) rather than an array. All MongoDB documents are maps in that sense. That said it never hurts to document it explicitly ;) – Remon van Vliet Apr 05 '12 at 08:34
-
@Adam Also, the very first sentence pretty much explains why PHP arrays are not arrays in the traditional sense "An array in PHP is actually an ordered map" ;). This is going slightly off-topic though. The answer is a bit misleading which was my main issue. – Remon van Vliet Apr 05 '12 at 14:54
-
i have a comment in my code that says `...even though mongo says order not necessarily preserved` - i was sure i read that somewhere on stackoverflow, anyone know what this could relate to? i'm searching for the post but haven't found it yet. **[ edit ]** perhaps it was this, re: `object property field order`: https://stackoverflow.com/a/8577139 – user1063287 Jul 04 '19 at 01:47
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

- 50,833
- 6
- 93
- 125
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.

- 1
- 1

- 7,816
- 4
- 54
- 50
-
6As an old question please read it better, it doesn't say anything about the order of the document fields. An object is not an array in javascript and arrays don't have fields (normally). – Francisco Presencia Oct 25 '15 at 19:47