Home > Software design >  Does a simple query guarantee ordered documents by _id
Does a simple query guarantee ordered documents by _id

Time:09-30

I have a collection of documents, the _id is an Int64 based on the DotNet's DateTime.Now.Ticks (with some additional logic to guarantee uniqueness).

Example document

{
"_id" : NumberLong(637680220534625572),
"variousProperties" : 123456,
}

If I do a simple query, such as querying all records between times:

collection.find({ "_id" : { "$lt" : NumberLong("637684488000000000"), "$gte" : NumberLong("637671528000000000") } )

Does the database guarantee that these documents will already be sorted by _id? Or do i have to also do .sort({_id: 1 }) ?

CodePudding user response:

By default index are crated on _id link if there are no other filter condition in the query it will always return sorted (ascending order)

  • Related