Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4028872
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:16:28+00:00 2026-05-20T11:16:28+00:00

The ObjectId used as the default key in mongodb documents has embedded timestamp (calling

  • 0

The ObjectId used as the default key in mongodb documents has embedded timestamp (calling objectid.generation_time returns a datetime object). So it is possible to use this generation time instead of keeping a separate creation timestamp? How will you be able to sort by creation time or query for the last N items efficiently using this embedded timestamp?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:16:28+00:00Added an answer on May 20, 2026 at 11:16 am

    I suppose since MongoDB ObjectId contain a timestamp, you can sort by ‘created date’ if you will sort by objectId:

    items.find.sort( [['_id', -1]] ) // get all items desc by created date.
    

    And if you want last 30 created items you can use following query:

    items.find.sort( [['_id', -1]] ).limit(30) // get last 30 createad items 
    

    I am actualy not sure,i just suppose that ordering by _id should work as described above. I’ll create some tests later.

    Update:

    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:

      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 <= 500; i++)
       {
         collection.Insert(new Item() { 
                 Id = ObjectId.GenerateNewId(), 
                 CreatedDate = DateTime.Now, 
                 Index = i });
       }
    
       var cursor = collection.FindAllAs<Item>();
       cursor.SetSortOrder(SortBy.Descending("_id"));
       var itemsOrderedById = cursor.ToList();
    
       var cursor2 = collection.FindAllAs<Item>();
       cursor2.SetSortOrder(SortBy.Descending("CreatedDate"));
       var itemsOrderedCreatedDate = cursor.ToList();
    
       for (int i = 0; i <= 500; i++)
       {
         Assert.AreEqual(itemsOrderedById[i].Index, itemsOrderedCreatedDate[i].Index);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: object that has object_id column and avalues that have object_id
When the p function is used to print out an object, it may give
Simple mongodb collection which stores sent emails: { msgid: objectid senderid: <some objectid>, sendername:
I used to compare lists like this, but it returns false in a test:
I have a Django project, that has a Address model. This is used in
I have a table named Info of this schema: int objectId; int time; int
When I attempt to do something like SELECT Max(ObjectId) FROM Objects; I see that
I have the following link function MyLinqToSQLTable.Where(x => x.objectID == paramObjectID).ToList(); I most of
I want to do something like this create type Item as object ( id
I have a table declaration: TYPE PERSON_TYPE AS OBJECT (ID NUMBER(38), NAME VARCHAR2(20)); TYPE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.