0

The objectID object used in mongo seems to be a little difficult to deal with when it comes to passing it back and forth with json and communicating with other applications. It seems that to use it, I have to convert back and forth between the object for querying and the string for json message passing.

I think it would be great instead for node-mongo-native to create my _id's as strings by default. Something like this would make a good unique id generation system that also doubles as a timestamper:

function createID(){
   return (Date.now() + ((Math.round(Math.random()*1000000))/1000000) ).toString();
}

Is there a way for me to have node-mongo-native use this function for id generation instead of the default?

RobKohr
  • 6,611
  • 7
  • 48
  • 69
  • 1
    Why not just use something like Mongoose that takes care of the ObjectID/string casting for you? – JohnnyHK Sep 29 '13 at 16:08
  • 1
    Or, just set the `_id` property yourself to your function. You don't have to let the driver set it. FYI -- your function will produce duplicates as written. `ObjectId`s should not. – WiredPrairie Sep 29 '13 at 17:46

2 Answers2

2

I never encountered the problems you described with having to convert ObjectID's, but I probably use a different technology stack than you do.

The _id field of a MongoDB document is only generated automatically as an ObjectId when the object you save to the database doesn't already contain a field named _id. When this field already exists in the saved document, its value will be used as _id. So when you want to generate and assign id's manually, you can do so.

But I would suggest you to rethink your algorithm for generating ID's, because it won't generate guaranteed unique ID's. It seems like you are reinventing a solution for a problem which is already solved - solved in the ObjectID generation of your database driver. When you have problems using the ObjectID as type ObjectID and would rather use a string, then why not generate an ObjectID and convert it to a string before assigning it to _id of your document?

 yourDocument._id = new ObjectID().toHexString();
Philipp
  • 67,764
  • 9
  • 118
  • 153
  • That is a good idea. Is there a way to have the driver auto-create new object ids for inserts that are hexstrings? I would rather not have to set ids whenever I save a new object. – RobKohr Sep 30 '13 at 19:01
  • @RobKohr maybe there is something you can do with some `.prototype` trickery. Or you could create an own wrapper function which sets the `_id` and then calls `collection.insert`. – Philipp Sep 30 '13 at 20:41
1

I never had problems passing back and forth because it is automatically casted to string as shown in these examples:

> var ObjectID = require('mongodb').ObjectID
undefined

> ObjectID.createPk()
52fcb07f46d17400006f44ac

> typeof ObjectID.createPk()
'object'

> JSON.stringify({_id: ObjectID.createPk()})
'{"_id":"52fcb09346d17400006f44ae"}'

> ObjectID.createPk().toString()
'52fcb09d46d17400006f44af'

The only thing you should take care is when writing a query (wether to fetch records or to update things) with _id, I usually do this:

exports.getCustomerByID = function (id, callback) {
  db.collection('customers').findOne({
    _id: new ObjectID(id.toString())
  }, callback);
};

This function allows to pass an id as an hex string or as a mongodb ObjectID.

José F. Romaniello
  • 13,866
  • 3
  • 36
  • 38