ALL COVERED TOPICS

NoSQL Benchmarks NoSQL use cases NoSQL Videos NoSQL Hybrid Solutions NoSQL Presentations Big Data Hadoop MapReduce Pig Hive Flume Oozie Sqoop HDFS ZooKeeper Cascading Cascalog BigTable Cassandra HBase Hypertable Couchbase CouchDB MongoDB OrientDB RavenDB Jackrabbit Terrastore Amazon DynamoDB Redis Riak Project Voldemort Tokyo Cabinet Kyoto Cabinet memcached Amazon SimpleDB Datomic MemcacheDB M/DB GT.M Amazon Dynamo Dynomite Mnesia Yahoo! PNUTS/Sherpa Neo4j InfoGrid Sones GraphDB InfiniteGraph AllegroGraph MarkLogic Clustrix CouchDB Case Studies MongoDB Case Studies NoSQL at Adobe NoSQL at Facebook NoSQL at Twitter

NAVIGATE MAIN CATEGORIES

Close

MongoDB Tips & Tricks: Using MongoDB ObjectIds as created-on timestamps

One of my favorite MongoDB tricks is the ability to use an ObjectId (the default type for MongoDB’s _id primary key) as a timestamp for when a document was created. Here’s how it works:

  $ import pymongo
  $ db = pymongo.Connection().test
  $ db.test.insert({'hello': 'world'})
  ObjectId('4f202e64e6fb1b56ff000000')
  $ doc = db.test.find_one()
  $ doc['_id'].generation_time
  datetime.datetime(2012, 1, 25, 16, 31, 32, tzinfo

Mike Dirolf used to work for 10gen so he probably knows quite a few such MongoDB tips & tricks.

Original title and link: MongoDB Tips & Tricks: Using MongoDB ObjectIds as created-on timestamps (NoSQL database©myNoSQL)

via: http://blog.fiesta.cc/post/16470048697/using-and-abusing-mongodb-objectids-as-created-on