1

Does default find() implicitly sort by _id?

In other words, are 2 mongo lines listed below equivalent?

db.collection.find().sort( { "_id" : 1 } )

db.collection.find()

lllll
  • 37
  • 1
  • 5

1 Answers1

4

The cursors uses the natural order if there is no sort defined.

https://docs.mongodb.com/manual/reference/method/cursor.sort/#return-natural-order

Result Ordering

Unless you specify the sort() method or use the $near operator, MongoDB does not guarantee the order of query results.

Return in Natural Order

The $natural parameter returns items according to their natural order within the database. This ordering is an internal implementation feature, and you should not rely on any particular structure within it.

Most of the time it's the insertion order, but that's not guaranteed.

Community
  • 1
  • 1
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107