MongoDB objectId references

Viewed 4243

I have one collection, with the id of the documents set as MongoDb Object Ids (so appear in the db as:

Collection1

    "someId": {
        "$oid": "5003cb802e28076412000001"
    },

In another collection, I am referencing these. However sometimes these references appear to be stored as proper oids:

Collection 2

    "someForiegnId": {
        "$oid": "5003cb802e28076412000001"
    },

But other times they have made it into the db as a normal string.

Collection 2    

    "someForiegnId": "5003cb802e28076412000001",

My question is - Is is important to store these foreign references in the oid format, or can they just be strings?

1 Answers

I know that I am answering a 1 year old question, but still.

It is always desirable to be consistent in your database. No matter how you store your data (for example of IP address as a string "87.123.12.12" an array [87, 123, 12, 12] or a number 1467681804) it should be always the same way. The same with your data: you have to select one format and stick with it.

The format you will select have implications on how much storage will you use and how fast you will be able to query the data. And the best way is to store them as ObjectID for the following reasons:

  • it takes only 12 bytes to store objectID whereas it will take you twice to store the same in the string. For sure this is a small difference, but it goes absolutely for free. Moreover in the world where you try to fit all your data in memory (or at least as much as possible) this is a good consideration. So you save not only HDD but RAM as well
  • you can easily get timestamp from your ID. Sometimes it might be useful to get when the object was created. With strings you can not do this
  • if you will decide to create an index based on this field - the size of it will be much smaller and you will query it much faster (because this is a number) then querying by a string.

So even if I would have only string representations - I would change to ObjectID(), in your case this is definitely worth switching (I know that you most probably have already done so).

P.S. You can modify the field by modifying the query in the following answer.

Related