Adam Monsen
Adam Monsen

Reputation: 9420

Are arrays in MongoDB documents always kept in order?

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?

Upvotes: 1

Views: 245

Answers (1)

Blacksad
Blacksad

Reputation: 15422

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.

Upvotes: 3

Related Questions