-1

According to this answer,

The results are returned in the order they are found, which may coincide with insertion order (but isn't guaranteed to be)

I am concerned with order of documents in result. Doesnt matter how they are stored and indexed.

For example,

If I issue a query and I got result in order

[id1, id3, id6, id2]

Again I re-issued the same query then order may change or not?

It is fine if I get result like below,

[id1, idx, id6, id2] //Because I inserted idx and deleted id3

I expect id1 should always appear before id6, and id6 before id2. I am using mongodb in sharded environment.

Community
  • 1
  • 1
Nilesh
  • 2,089
  • 3
  • 29
  • 53
  • Which of course means there is no other different response. Natural order is what is returned unless you explicitly asked for some other sort order. So the answer you already found is the answer to this question. Results will not change, until the order on disk is changed from a natural result. But it can happen, so you should never "presume" that the order you got in one query without a sort will stay the same every time you query. – Blakes Seven Mar 23 '16 at 14:00

1 Answers1

2

Do MongoDB returns query results always in same order?

The short answer is: No.

I presume we're talking about cases when explicit sort() method is not used. In this case, you can expect for the same results order only if you are querying a capped collection.

Roman
  • 64,384
  • 92
  • 238
  • 332