Zhe Chen
Zhe Chen

Reputation: 2957

Order of results for `sort` using mongoose

If I have two equal values for a field. What would be the order of results for sort on that field? Random or ordered by insertion date?

Upvotes: 1

Views: 206

Answers (1)

felix
felix

Reputation: 9285

If two documents have equal values for the field you're sorting on, then MongoDB will return the results in the order they are found on disk (ie Natural order)

from MongoDB Documentation :

natural order:

The order in which the database refers to documents on disk. This is the default sort order. See $natural and Return in Natural Order.

This may coincide with insertion date in some case, but not all of the time (especially when you perform insertion/deletion on your collection), so you should assume that this is random ordering

Upvotes: 1

Related Questions