1

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?

Adam Monsen
  • 9,054
  • 6
  • 53
  • 82
  • possible duplicate of [Do arrays stored in MongoDB keep their order?](http://stackoverflow.com/questions/9013916/do-arrays-stored-in-mongodb-keep-their-order) – Adam Monsen Apr 03 '12 at 19:44

1 Answers1

3

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.

Community
  • 1
  • 1
Blacksad
  • 14,906
  • 15
  • 70
  • 81
  • 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? https://groups.google.com/d/topic/mongodb-user/HfFlSInmZME/discussion seems like the best lead so far. – Adam Monsen Apr 03 '12 at 19:43