Roman Puchkovskiy
Roman Puchkovskiy

Reputation: 11835

Is natural order in MongoDB stable?

MongoDB documentation https://docs.mongodb.com/manual/reference/glossary/#term-natural-order defines natural order as

The order in which the database refers to documents on disk. This is the default sort order.

Also, here https://docs.mongodb.com/manual/reference/method/cursor.sort/#return-natural-order it says the following:

This ordering is an internal implementation feature, and you should not rely on any particular structure within it.

So, can the natural ordering change under the following circumstances?

  1. Normal operation: compactions, replication, backup/restore, node bootstrap from healthy replicas
  2. Storage engine upgrade
  3. Something else?

I mean, if a have a query that does not specify an ordering (or requests $natural ordering), is it possible for an existing collection, for the data that remains untouched, to be returned in a different order by that query after some time? Are there any guarantees?

Upvotes: 2

Views: 1134

Answers (1)

D. SM
D. SM

Reputation: 14480

Are there any guarantees?

None whatsoever.

is it possible for an existing collection, for the data that remains untouched, to be returned in a different order by that query after some time?

Yes. This possibility is allowed by the published documentation. It's unlikely for practical reasons but if the database decided to shuffle the order of documents one day out of the blue it wouldn't run afoul of any promises it made in published documentation.

Normal operation: compactions, replication, backup/restore, node bootstrap from healthy replicas

In addition to those, inserting a single document can change the order of existing documents.

Upvotes: 4

Related Questions