4 ms·
Here's two: (1) There's no need to add a "created" field on your documents. You can extract it from the _id field by just taking the first 4 bytes. (2) If you
by alexmic 14y ago
Here's two:
(1) There's no need to add a "created" field on your documents. You can extract it from the _id field by just taking the first 4 bytes.
(2) If you are storing hashes (md5 for example), you might want to consider storing them as BinData instead of strings. Mongo uses UTF-8 so every character will be at least 8 bits whereas you can get away with 4 bits per character.
- ukd1 14y agoGreat points but they are not really gotchas; I was trying to help people avoid big / 'obvious' / documented things when using MongoDB :-)
- alexmic 14y agoSure :)
- rsanders 14y agoCan you efficiently select and sort on the date part of the _id?
- mattparlane 14y agoSorting 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.