Are arrays in MongoDB documents always kept in order?

Go To


I can find ample evidence that MongoDBs are always kept in the order given when a document is inserted or updated, but I just can't find it explicitly spelled out in the documentation for MongoDB.

So I'll go ahead and ask the [probably stupid] question: are arrays in MongoDB documents kept in the order specified when they were inserted/updated?

And sharding/replication/etc. do not effect this ordering, correct?

2012-04-03 19:26
by Adam Monsen
possible duplicate of Do arrays stored in MongoDB keep their order?Adam Monsen 2012-04-03 19:44


If you are speaking about the order of an array value of a document field, then yes the order is always kept. See this question.

If you speak about the order of documents in a collection, to my knowledge you shouldn't count on any order. If you need an order, you should implement your own created_at or sequence_index field.

2012-04-03 19:30
by Blacksad
Yes, I'm asking about the order of an array on a document. That question looks like a duplicate, thanks for finding it! Do you have a reference besides that stackoverflow post? For instance, something in a spec, the MongoDB docs, their mailing list, etc? seems like the best lead so far - Adam Monsen 2012-04-03 19:43