Does MongoDB resort the collections?

Hi

We created a new collection and we inserted documents in batches (500 documents) with consecutive numbers (10000, 10001, 10002,… 10XXX). We checked on Mongo Atlas and Mongo Compass and the documents were inserted in order. I also checked the order during the next few days and the order was fine. However, I checked them this morning and the order is wrong.

The documents with the numbers 10217, 10218, 10219 and 10220 already exist but they are several pages after. As I said, we confirmed that the documents were sorted before. The field contactNumber is an index.

Do you know if MongoDB “re-sorts” the documents in the collections (for performance purposes or whatever)? I’m looking at the Mongo documentation and Google, but it’s not clear.

We are using Nodejs & Mongoose (5.12.3), and MongoDB 5.0.18.

Our backend logic is querying this collection by findAndDelete to pick the first document with the first number available, so currently is not picking the lowest number available. We can add a sort by contactNumber to fix this but first, we want to understand why this “re-sort” happened.

Thank you.

Hey :wave: @Jose_Antonio_Herrera,

Thank you for reaching out to the MongoDB Community forums.

It’s worth noting that the order of storing documents in MongoDB is not guaranteed, nor is the view. It can change depending on the query you perform.

By default, MongoDB does not enforce any specific order on the documents within a collection. It’s important to note that without an explicit sort, the order in which documents are returned follows the natural order. The natural order is not guaranteed to match the insertion order, except in the special case of capped collections (which have significant usage restrictions ).

The natural order is not a stable sort order; it is determined “as documents are found”:

This ordering is an internal implementation feature, and you should not rely on any particular ordering of the documents.

If you require a predictable sort order for the retrieved documents, you must include an explicit sort() in your query and ensure unique values for the sort key. The natural order is described as an internal implementation detail because the storage engine decides how to store data most efficiently and it may not correspond to the order of insertion.

Therefore, I recommend not depending on the way the documents are viewed on your end, as the order in which the documents are returned could be different. Please refer to the findOneAndDelete() behavior to learn more.

I hope this clarifies your questions! Let us know if you need further help.

Best regards,
Kushagra

5 Likes

Thank you! We won’t depends on the way the documents are viewed. Anyways, I’m still surprised that the “view ordering” suddenly changed from one day to the next.

We launched a benchmark comparing findOneAndDelete() and findOneAndDelete({}, {sort: {contactNumber: 1}}) and the difference is minimum, even slightly faster with sort since contactNumber is an index.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.