Should I use the timestamp in "_id"? Should I use the timestamp in "_id"? mongodb mongodb

Should I use the timestamp in "_id"?


I'd actually say it depends on how you want to use the date.

For example, it's not actionable using the aggregation framework Date operators.

This will fail for example:

db.test.aggregate( { $group : { _id:  { $year: "$_id" } } })

The following error occurs:

"errmsg" : "exception: can't convert from BSON type OID to Date"

(The date cannot be extracted from the ObjectId.)

So, operations that are normally simple date operations become much more complex if you wanted to do any sort of date math in an aggregation. It would be far easier to have a createDateTime stamp. Counting the number of documents created in a particular year and month would be simple using aggregation with a distinct createdDateTime field.

You can sort on an ObjectId, to some degree. The remaining 8 bytes of the ObjectId aren't sortable in a meaningful way. Most MongoDB drivers default to creating the ObjectId within the driver and not on the database. So, if you've got multiple clients (like web servers for example) creating new documents (and new ObjectIds), the time stamps will only be as accurate as the various servers.

Also, depending the precision you'd need, an ISODate value is stored using 8 bytes, rather than the 4 used in an ObjectId.


Yes, you should. There is no reason not to do, besides the human readability while directly looking into the database. See also here and here.

If you want to use the aggregation framework to group by the date within _id, this is not possible yet as WiredPrairie correctly sais. There is an open jira ticket for that, you might watch. But of course you can do this with Map-Reduce and ObjectID.getTimestamp(). An example for that can be found here.