Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suppose since MongoDB ObjectId contain a timestamp, you can sort by 'created date' if you will sort by objectId:</p> <pre><code>items.find.sort( [['_id', -1]] ) // get all items desc by created date. </code></pre> <p>And if you want last 30 created items you can use following query:</p> <pre><code>items.find.sort( [['_id', -1]] ).limit(30) // get last 30 createad items </code></pre> <p>I am actualy not sure,i just suppose that ordering by _id should work as described above. I'll create some tests later.</p> <p><strong>Update:</strong></p> <p>Yes it is so. If you order by _id you will automatically order by _id created date. I've done small test in c#, mb someone interest in it:</p> <pre><code> public class Item { [BsonId] public ObjectId Id { get; set; } public DateTime CreatedDate { get; set; } public int Index { get; set; } } [TestMethod] public void IdSortingTest() { var server = MongoServer.Create("mongodb://localhost:27020"); var database = server.GetDatabase("tesdb"); var collection = database.GetCollection("idSortTest"); collection.RemoveAll(); for (int i = 0; i &lt;= 500; i++) { collection.Insert(new Item() { Id = ObjectId.GenerateNewId(), CreatedDate = DateTime.Now, Index = i }); } var cursor = collection.FindAllAs&lt;Item&gt;(); cursor.SetSortOrder(SortBy.Descending("_id")); var itemsOrderedById = cursor.ToList(); var cursor2 = collection.FindAllAs&lt;Item&gt;(); cursor2.SetSortOrder(SortBy.Descending("CreatedDate")); var itemsOrderedCreatedDate = cursor.ToList(); for (int i = 0; i &lt;= 500; i++) { Assert.AreEqual(itemsOrderedById[i].Index, itemsOrderedCreatedDate[i].Index); } } </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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