ObjectId instantiation error

56 views
Skip to first unread message

cris

unread,
Sep 24, 2012, 7:18:41 PM9/24/12
to node-mong...@googlegroups.com
Hi all. I'm using the mongojs wrapper around mongodb node driver, and when i try to create a client-side id with new ObjectId() i get an error saying the argument must be a single string of 12 bytes or a string of 24 hex characters, so it seems that the ObjectId constructor is requiring an _id argument, but the api docs specify that without arguments ObjectId will create a new _id instance. What may be the issue here?

Thanks.

cris

unread,
Sep 24, 2012, 7:24:53 PM9/24/12
to node-mong...@googlegroups.com
Here's the code:

var  db = require('mongojs');
var dbd = db.connect('testdb');

var id = new db.ObjectId();

dbd.collection('docs').count( function (err, doc) {
        dbd.collection('docs').insert({"_id": id, "title": "My title", "content": "My content", function (err, doc) {
                if (err) {console.log(err);}
            });
        console.log('obj literal load to MongoDB');
});

christkv

unread,
Sep 25, 2012, 7:23:07 AM9/25/12
to node-mong...@googlegroups.com
better log this as an issue in the github tracker as it's not a driver issue

cris

unread,
Sep 25, 2012, 3:28:25 PM9/25/12
to node-mong...@googlegroups.com
Yes, it´s a mongojs issue; suits me for not reading their docs carefully where they state that mongojs try to emulate mongodb native driver "as much as possible". Here's the code where they implement ObjectId instantiation:

var createObjectId = function() {
if (!mongo.BSONNative || !mongo.BSONNative.ObjectID) {
return function(id) {
return mongo.BSONPure.ObjectID.createFromHexString(id);
};
}
return function(id) {
return new mongo.BSONNative.ObjectID(id);
};
}();

Reply all
Reply to author
Forward
0 new messages