Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The source for the MongoDB C# <code>CompareTo</code> is currently <a href="https://github.com/mongodb/mongo-csharp-driver/blob/master/MongoDB.Bson/ObjectModel/ObjectId.cs#L410" rel="nofollow">here</a>. The code compares each element of the <code>ObjectId</code> all the way to 3 byte counter. Given the nature of the <a href="http://docs.mongodb.org/manual/core/object-id/" rel="nofollow">ObjectId</a> containing: </p> <ul> <li>a 4-byte value representing the seconds since the Unix epoch</li> <li>a 3-byte machine identifier </li> <li>a 2-byte process id</li> <li>and a 3-byte counter, starting with a random value</li> </ul> <p>it doesn't make sense to sort beyond the time stamp. The <code>CompareTo</code> while it is accurate and will produce consistent results, may not sort in a manner that matches with your expectations.</p> <p>Given that there will be instances where two objects were created at the same timestamp (4 byte value), you'll have some variance then in the results given the way that the <code>CompareTo</code> in C# works. So, performing assertion you made will result in some confusing results and should not be used as a way of detecting an out of sequence result.</p> <p>Most drivers create the <code>_id</code>/<code>ObjectId</code> value when it's not present (including the C# driver). There's really not much you could sort on besides the timestamp. </p> <p>You could do:</p> <pre><code>Throw.Assert(id.Timestamp &gt; previous.Timestamp, "Sort order is invalid!"); </code></pre>
 

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