Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does MongoDB sort records when no sort order is specified?
    text
    copied!<p>When we run a Mongo find() query without any sort order specified, what does the database internally use to sort the results?</p> <p>According to the <a href="http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order" rel="noreferrer">documentation on the mongo website</a>:</p> <blockquote> <p>When executing a find() with no parameters, the database returns objects in forward natural order.</p> <p>For standard tables, natural order is not particularly useful because, although the order is often close to insertion order, it is not guaranteed to be. However, for Capped Collections, natural order is guaranteed to be the insertion order. This can be very useful.</p> </blockquote> <p>However for standard collections (non capped collections), what field is used to sort the results? Is it the <strong>_id</strong> field or something else?</p> <p><strong>Edit:</strong> </p> <p>Basically, I guess what I am trying to get at is that if I execute the following search query:</p> <pre><code>db.collection.find({"x":y}).skip(10000).limit(1000); </code></pre> <p>At two different points in time: <strong>t1</strong> and <strong>t2</strong>, will I get different result sets:</p> <ol> <li>When there have been no additional writes between t1 &amp; t2?</li> <li>When there have been new writes between t1 &amp; t2?</li> <li>There are new indexes that have been added between t1 &amp; t2?</li> </ol> <p>I have run some tests on a temp database and the results I have gotten are the same (<strong>Yes</strong>) for all the 3 cases - but I wanted to be sure and I am certain that my test cases weren't very thorough.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload