-
Type: Improvement
-
Resolution: Done
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Internal Code
-
Labels:None
I'm having an issue I think is likely faced by many many developers using mongo: two _id formats (hex string and ObjectId).
The issue is described in both of these stackoverflow questions:
http://stackoverflow.com/questions/23188490/best-practices-for-dealing-with-objectid-with-mongo-and-javascript
http://stackoverflow.com/questions/27979012/best-ways-to-provide-a-very-open-ended-api-over-mongo-db-with-regards-to-objec
I would like to suggest that mongo unify strings and ObjectIds. I am not suggesting that all _ids be strings. What I'm suggesting is that mongo convert any string that is a valid ObjectId representation to an ObjectId for storage, and on retrieval, convert any ObjectId to a string to pass to the end-user (client-side or server-side for the server-to-user part doesn't matter).
This way you get a couple effects:
- Users no longer have to manually convert between ObjectIds and their string representations when _ids need to be serialized.
- Certain random strings that aren't intended to be ObjectIds get compressed for storage (not a problem, or much of a benefit)
- There is a (negligible) cost of serializing and deserializing ObjectIds
Currently, the only solution I have for my use-case is to have a large class of _ids referencing external objects (no primary keys tho) that are stored as strings rather than ObjectIds. This is obviously not ideal, which is why I'm suggesting a core feature for mongo.