Documents being rearranged on their own?

Lately I’ve noticed the order of my documents in collections being changed automatically. I haven’t been able to spot any pattern in it - they change seemingly randomly and then eventually revert back to normal. These are documents that aren’t even being modified.

Any idea what’s causing this behaviour? Thanks!

1 Like

Hi @TemeS,

Why is it a problem if you don’t expect them to be in a certain order?
In theory, docs are supposed to stay in the same natural order if the collection is read only.
I’m not a pro of the WiredTiger internals but I don’t see a reason why the engine would reorder docs randomly without any write operation.

If you expect the docs in a non-random order, I would advice to create an index and add a sort to your query to “sort” this problem. :slight_smile:

If you just always want them in the same order and you don’t care which one it is, I would sort on the _id field (and the index already exists so it’s free).

Cheers,
Maxime.

1 Like

Hey,

thanks for the reply! The order they were originally implemented in is the order that they’re meant to be presented in the app. But an even bigger problem is that as my server and clients both read from those same documents, them getting “randomly” rearranged means that the clients and server will have different views on the data.

The app in question is in closed development so nothings urgently on the line here but I’m quite confused by the behaviour. There’s definitely no write actions happening on those documents.

I could just sort the data when querying it but it’s strange that the initial order of documents isn’t preserved.

It is the only way to present data in a consistent order.

It is not strange for some. For example, I can easily imagine that documents already in the server cache are sent to the client first while the server reads other documents from disk to prepare the next batch. Otherwise, you flush the current documents from the cache to read the documents of the consistent order, and then you reread the documents that were already in the cache because they are next in the consistent order.

If you do not sort, the server assume rightfully that you do not care about the order, and make sure it does the least work possible to handle your request so that it has more cpu cycle to handle requests where order is important and specified.

3 Likes

What do you mean by “document order”? Do you mean that the documents are returned from collection in different order or do you perhaps mean that inside the document the fields are in different order than they were in when you created the document?

Asya

I think I have the exact issue today when the documents within my collection rearranged themselves to a different order for some reason. It would be good to see what caused this, as for me, this is the first that I see documents rearrange themselves.

At first, I was suspecting that there is a security issue, where a hacker was rearranging the order. =X

Documents have no specific order. If you want to see the documents in a specific order you need to sort them.

2 Likes