[SERVER-16909] Unify ObjectIds and strings Created: 16/Jan/15  Updated: 17/Jan/15  Resolved: 17/Jan/15

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Billy Tetrud Assignee: Ramon Fernandez Marina
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

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.



 Comments   
Comment by Ramon Fernandez Marina [ 17/Jan/15 ]

fresheneesz, this proposal would break lexicographical ordering of strings or double the size of OID keys in indexes, and it would force the server to examine all string-type data to see if it was eligible for conversion to OID types.

Generated at Thu Feb 08 03:42:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.