How does MongoDB sort records when sort order is not set?

When we run the Mongo find () query without specifying the sort order, what internal database is used to sort the results?

According to the documentation on mongo website :

When performing a search () without parameters, the database returns objects in the direct natural order.

For standard tables, the natural order is not particularly useful, because although the order is often close to the order of placement, this is not guaranteed. However, for Capped Collections, the natural order is the guaranteed insert order. This can be very helpful.

However, for standard collections (uncovered collections), which field is used to sort the results? Is this _id field or something else?

Edit:

Basically, I assume that I'm trying to understand that if I performed the following search query:

db.collection.find({"x":y}).skip(10000).limit(1000); 

At two different points in time: t1 and t2 , I get different result sets:

  • If there was no additional entry between t1 and t2?
  • When did the new entries between t1 and t2 appear?
  • Are there new indexes added between t1 and t2?

I performed several tests in the temp database and the results I got are the same ( Yes ) for all three cases - but I wanted to be sure, and I'm sure my test cases were not very thorough.

+58
mongodb
Jul 22 '12 at 9:20
source share
2 answers

By definition, sorting is undefined by default , as well as the order in which documents are returned. If the request is missing, it will use the natural order . Results are returned in the order in which they are found , which may coincide with the insertion order (but not guaranteed) or the order of the index used.

Some examples that will affect the storage order (natural):

  • If the documents are updated and do not correspond to their allocated space, they will be moved.
  • new documents can be inserted into existing spaces created by deleted or moved documents.

If an index is used, documents will be returned in the order in which they are found. If more than one index is used, then the order depends on the internal index by which the index first identified the document during the deduplication process.

If a specific order is required , then you must enable sorting with your request.

The exception noted for the limited order of collections is that documents cannot be moved and are stored in the order of placement. An order is part of a limited set that ensures that the oldest documents β€œgrow” in the first place. In addition, documents cannot be deleted or moved in the collection (see Use and Limitations for more information).

+63
Jul 22 2018-12-12T00:
source share

It is returned in the stored order (order in the file), but it is not guaranteed that they are in the inserted order. They are not sorted by _id field. Sometimes it may look like it is sorted by insertion order, but it may change in another query. This is unreliable.

+3
Jul 22 2018-12-12T00:
source share



All Articles