Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was typing the same as Daimon's first answer, using <code>sort</code> and <code>limit</code>. This is probably not recommended, especially with some drivers (which use random numbers instead of increments for the least significant portion), because of the way the _id is generated. It has second [as opposed to something smaller, like millisecond] resolution as the most significant portion, but the last number can be a random number. So if you had a user save twice in a second (probably not likely, but worth noting), you might end up with a slightly out of order latest document.</p> <p>See <a href="http://www.mongodb.org/display/DOCS/Object+IDs#ObjectIDs-BSONObjectIDSpecification" rel="nofollow">http://www.mongodb.org/display/DOCS/Object+IDs#ObjectIDs-BSONObjectIDSpecification</a> for more details on the structure of the ObjectID.</p> <p>I would recommend adding an explicit versionNumber field to your documents, so you can query in a similar fashion using that field, like so:</p> <pre><code>db.coll.find({documentId: &lt;id&gt;}).sort({versionNum: -1}).limit(1); </code></pre> <p><strong>edit to answer question in comments</strong></p> <p>You can store a regular DateTime directly in MongoDB, but it will only store the milliseconds precision in a "DateTime" format in MongoDB. If that's good enough, it's simpler to do.</p> <pre><code>BsonDocument doc = new BsonDocument("dt", DateTime.UtcNow); coll.Insert (doc); doc = coll.FindOne(); // see it doesn't have precision... Console.WriteLine(doc.GetValue("dt").AsUniversalTime.Ticks); </code></pre> <p>If you want .NET DateTime (ticks)/Timestamp precision, you can do a bunch of casts to get it to work, like:</p> <pre><code>BsonDocument doc = new BsonDocument("dt", new BsonTimestamp(DateTime.UtcNow.Ticks)); coll.Insert (doc); doc = coll.FindOne(); // see it does have precision Console.WriteLine(new DateTime(doc.GetValue("dt").AsBsonTimestamp.Value).Ticks); </code></pre> <p><strong>update again!</strong></p> <p>Looks like the real use for BsonTimestamp is to generate unique timestamps within a second resolution. So, you're not really supposed to abuse them as I have in the last few lines of code, and it actually will probably screw up the ordering of results. If you need to store the DateTime at a Tick (100 nanosecond) resolution, you probably should just store the 64-bit int "ticks", which will be sortable in mongodb, and then wrap it in a DateTime after you pull it out of the database again, like so:</p> <pre><code>BsonDocument doc = new BsonDocument("dt", DateTime.UtcNow.Ticks); coll.Insert (doc); doc = coll.FindOne(); DateTime dt = new DateTime(doc.GetValue("dt").AsInt64); // see it does have precision Console.WriteLine(dt.Ticks); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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