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

| | bookmark | email

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

tags:mongodb

via NoSQL databases