Reputation: 11835
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?
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
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