Store related keys as OID or strings in MongoDB

My mongodb setup keeps my data mostly abnormal, I only store enough user data that I would like to display with a message. But I still always keep the _id of the associated user or the related post to say the vote. Some of the identifiers come from ajax calls (ultimately stored as strings), while others come directly from the mongodb server (they are stored in the data type obtained from the OID), so right now when I save them, I have a combination related identifiers like strings or OIDs. My questions are being washed, should I convert them all to strings or all to OIDs?

+1
source share
1 answer

ObjectIds They are more efficient in terms of space, and comparing ObjectIds is faster than comparing strings. You should always convert the incoming string back to ObjectId (e.g. oid = new ObjectId (string)). I can't think of an exception where you need strings.

+3
source

Source: https://habr.com/ru/post/1442038/


All Articles