Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Handling Documents that do not have ObjectID #11

Closed
mgd216 opened this issue Mar 31, 2011 · 2 comments
Closed

Handling Documents that do not have ObjectID #11

mgd216 opened this issue Mar 31, 2011 · 2 comments
Labels

Comments

@mgd216
Copy link

mgd216 commented Mar 31, 2011

I have a program that is storing documents in MongoDB, but the program is creating it's own object ID (which is just a UUID). See example documents:

{ "_id" : "e5e781f9-2947-4198-adca-f4d452e98c1a", "isDone" : false, "description" : "New Task" }
{ "_id" : "07cea259-f333-4836-a53f-644d2428a382", "isDone" : false, "description" : "New Task" }
{ "_id" : "1dcf6de5-c634-44c4-b5d9-9e349612ed46", "isDone" : false, "description" : "New Task" }
{ "_id" : "dfd31230-4293-4539-aa55-292297474ec4", "isDone" : false, "description" : "New Task" }

When trying to pull these documents through mongodb-rest, I receive the following error:

node.js:178
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Object e5e781f9-2947-4198-adca-f4d452e98c1a has no method 'toHexString'
at Object.flavorize (/usr/local/lib/node/.npm/mongodb-rest/0.6.7/package/lib/util.js:22:25)
at /usr/local/lib/node/.npm/mongodb-rest/0.6.7/package/lib/rest.js:50:32
at Array.forEach (native)
at /usr/local/lib/node/.npm/mongodb-rest/0.6.7/package/lib/rest.js:49:18
at /usr/local/lib/node/.npm/mongodb/0.9.2/package/lib/mongodb/cursor.js:122:15
at /usr/local/lib/node/.npm/mongodb/0.9.2/package/lib/mongodb/cursor.js:167:11
at /usr/local/lib/node/.npm/mongodb/0.9.2/package/lib/mongodb/cursor.js:467:28
at [object Object].close (/usr/local/lib/node/.npm/mongodb/0.9.2/package/lib/mongodb/cursor.js:625:17)
at [object Object].nextObject (/usr/local/lib/node/.npm/mongodb/0.9.2/package/lib/mongodb/cursor.js:467:10)
at Array. (/usr/local/lib/node/.npm/mongodb/0.9.2/package/lib/mongodb/cursor.js:161:12)

On line 21 of the /lib/util.js package, a suggestion would be to put another 'if' statement to check if the _id is a Mongo ObjectID or just a string. Something like:

if (id == ObjectID) {
doc._id = doc._id.toHexString();
} else {
// leave doc._id alone
}

Just a suggestion, this could be a rare case since most people let Mongo (or the driver) create the ObjectID. This just happens where the project I'm on, I'm given a unique ID that is mapped to other data sources outside of Mongo via this UUID.

Great work, thanks.

Matt

@samyakbhuta
Copy link
Contributor

+1 to mgd216,
Here it is assumed that the _id would be always going to be of BSON type ObjectID, which is false assumption. The assumption is made at following links, and other places as well.

Guess, one should be able to dictate using config.js ( or mongodb-rest should be able to do a smart guess) as what is the BSON type for _id field. E.g when it comes to uuid as values for _id, the BSON Binary type with subtype as UUID should be used ( See http://bsonspec.org/#/specification )

Also, node-mongodb-native already provide the support for various types. See https://github.com/christkv/node-mongodb-native/blob/master/examples/types.js#L30 and https://github.com/christkv/node-mongodb-native/blob/master/external-libs/bson/binary.cc#L21.

Please, also see the discussion regarding this issue at http://groups.google.com/group/node-mongodb-native/browse_thread/thread/73e0a02b5dd08102.

@mgd216, any specific reason why you don't want to store UUIDs as BSON Binary type with subtype UUID ? Why you have choose BSON type string to store them, it will occupy twice the space that too with indexing overhead.

@samyakbhuta
Copy link
Contributor

Also, at https://github.com/tdegrunt/mongodb-rest/blob/master/lib/rest.js#L74 assuming that the _id would have equivalent hex string is not helpful for anybody who is dealing with BSON types other than ObjectIds.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants