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

  • SEARCH
  • Home
  • 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 6982585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:18:27+00:00 2026-05-27T18:18:27+00:00

John Nunemaker has a blog post with some nice tips about Mongo ObjectIds —

  • 0

John Nunemaker has a blog post with some nice tips about Mongo ObjectIds — http://mongotips.com/b/a-few-objectid-tricks/ — in particular I was interested in the tip about generation_time. He suggests it’s not necessary to explicitly store the created_at time in mongo documents because you can always pull it from the ID, which caught my attention. Problem is I can’t figure out how to generate mongo queries in mongomapper to find documents based on creation time if all I have is the id.

If I store a key :created_at as part of the document I can do a query in mongomapper to get all documents created since Dec 1st like this:

Foo.where(:created_at.gt=>Time.parse("2011-12-01"))

(which maps to:

{created_at: {"$gt"=>Thu Dec 01 06:00:00 UTC 2011}}

I can’t figure out how to make the equivalent query using the ObjectId.. I imagine it’d look something like this (though obviously generation_time is a ruby function, but is there an equivalent I can use on the objectid in the context of a mongo query?)

Foo.where('$where'=>"this.id.generation_time > new Date('2011-12-01')")
{$where: "this.id.generation_time > new Date('2011-12-01')"}

One further question: if I forgo storing separate timestamps, will I lose the timestamp metadata if I dump and restore my database using mongodump? Are there recommended backup/restore techniques that preserve ObjectIds?

  • 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-27T18:18:28+00:00Added an answer on May 27, 2026 at 6:18 pm

    this is javascript code which would be run in the shell but generation time is a mongomapper method so it doesn’t make sense in the code you have.

    In rails you would get the id by saying something like

    created_at = self.id.generation_time.in_time_zone(Time.zone)
    

    Where self refers to an instance of Foo.

    And you would query by saying

    Foo.find('_id' => {'$gte' => BSON::ObjectId.from_time(created_at)}).count
    

    Why bother though… the hassle isn’t worth it, just store the time.

    Regarding the backup/restore techniques, unless you are manually reading and re-inserting mongodump/restore and similar tools will preserve the object id so you have nothing to worry about there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

John Nunemaker recently blogged about the various ways to define class methods in Ruby,
I'm downloaded the code from John Papa's book here: http://silverlight-data.com/ and am sucessfully running
John Resig’s Simple Javascript Inheritance: http://ejohn.org/blog/simple-javascript-inheritance/ I tried to do such thing: var SomeClass
I'm reading the John Resig blog about the Strict Mode in javascript, but i
One table has John Doe <jdoe@aol.com> while another has jdoe@aol.com . Is there a
John Regehr's blog post A Guide to Undefined Behavior in C and C++, Part
A recent post by John Gruber notes that the following legalese: 3.3.1 — Applications
Example: names = ['James John', 'Robert David', 'Paul' ... the list has 5K items]
Assuming a collection like this: People { name: John Doe, email: John Doe <john@doe.com>,
I have the following string: Mr John Smith Dickson <john@yahoo.com> I want to split

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.