Having _id which are more than 12 bytes long

1,691 views
Skip to first unread message

Samyak Bhuta

unread,
May 12, 2011, 3:37:48 AM5/12/11
to node-mongodb-native, sam...@junesoftware.com
Hello Community,

Error : "Argument passed in must be a single String of 12 bytes or a
string of 24 hex characters in hex format"

Yes, I am trying to a use an hex string for my _id field which is not
24 char long (12 bytes), but a 32 char hex string (16 bytes), as I am
using uuid instead of implicitly generated _id by mongodb. As far as
mongodb is concerned, having _id not set as ObjectID ( BSON's object
id ) is perfectly alright.

So, should I conclude that the node-mongodb-native has assumed that
all the user of the driver would be relying on implicitly generated
Object-ids for _id field ? If yes, is there any specific reason to be
enforce this ? Could I write an additional method in objectid.h and
objectid.cc that support uuids ? Would it be a wise thing to do ?

I see, using uuids as recurring scenario in many application
development projects, and would be great to have it along !!!

Samyak Bhuta




christkv

unread,
May 12, 2011, 4:00:39 AM5/12/11
to node-mongodb-native
The _id's are generated by the driver not mongodb. You can assign any
value you want to the _id field as long as it's a valid BSON type and
is unique. the driver assigns one for you in case you don't have one
for convenience. So no need to hack anything.

collection.insert({_id : "12345678123456781234567812345678", field:
'1'}, {safe:true}, function(err, result) {
test.equal(null, err);

collection.find({_id :
"12345678123456781234567812345678"}).toArray(function(err, items) {
test.equal(null, err);
test.equal(items[0]._id, "12345678123456781234567812345678")
test.equal(items[0].field, '1')

finished_test({insert_doc_with_uuid:'ok'});
})
});

Samyak Bhuta

unread,
May 12, 2011, 5:51:25 AM5/12/11
to node-mongodb-native
@christkv,

I just have a doubt following to your example code. In the example
specified UUID will be inserted as a string, occupying twice the
space. I could see that we can use BSON Binary type to save space. I
could also see the example at https://github.com/christkv/node-mongodb-native/blob/master/examples/types.js#L30.

But, I could not make out how we can even specify the subtype for the
same (in my case it would be UUID) ? I think I am not able to make out
the syntax for that. What I would like to do is same as described at
http://blog.mikeobrien.net/2010/08/working-with-guids-in-mongodb-with-ruby.html
.


I would also like to bring to your attention the code at ...
* https://github.com/tdegrunt/mongodb-rest/blob/master/lib/rest.js#L23
* https://github.com/tdegrunt/mongodb-rest/blob/master/lib/rest.js#L91
* https://github.com/tdegrunt/mongodb-rest/blob/master/lib/rest.js#L109

It looks that the the author has assumed that _id would be always of
BSON type ObjectId, which is not the case always.


Thanks for your prompt response. It was helpful and valuable.

Cheers,

Samyak
Reply all
Reply to author
Forward
0 new messages