Skip to content

Instantly share code, notes, and snippets.

@amitsaxena
Last active February 7, 2017 18:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitsaxena/6977506 to your computer and use it in GitHub Desktop.
Save amitsaxena/6977506 to your computer and use it in GitHub Desktop.
How to get object creation timestamp from BSON object id using mongoid, and do range queries on it.
# To get the created at timestamp
sample_object.id.generation_time
# To do range queries
start = Moped::BSON::ObjectId.from_time(Time.now.beginning_of_day)
finish = Moped::BSON::ObjectId.from_time(Time.now.end_of_day)
MyModel.where(:id => {'$gt' => start, '$lt' => finish}).count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment