Skip to content

Comment on MongoDB Gotchas and How To Avoid Themparent

Comments

Can you efficiently select and sort on the date part of the _id?

Sorting is easy, you just choose _id as the sort field.

Selecting is also reasonably easy, the first 4 bytes of the id are the timestamp (seconds since the epoch). You just create a hex string in that format -- 4 bytes of timestamp and then 8 bytes of zeroes and then create an object ID (using the classes provided by your driver) and do:

    coll.find({_id:{$gte:<id>}})
or whatever is the equivalent in your language of choice.
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.