strongloop/loopback-connector-mongodb — pull request #17 by pandaiolo (2014-02-19 01:12:16+00:00)
Title: Fix for text id object insertion issue

Mongodb connector uses an `save/upsert` operation to insert new document, with the following code :

var oid = ObjectID(idValue);
delete data[idName];
this.collection(model).update({_id: oid}, data, {safe: true, upsert: true}, function (

The data yet to be inserted does not contains an `_id` property, and the regular `id` is deleted, so the final object in the database will have a mongodb-generated id, even when a string id was provided.

(behaviour with mongod v2.4.9)

Here is a fix that makes sure user-defined id is set as mongo _id :

var oid = ObjectID(idValue);
data._id = oid;
delete data[idName];
this.collection(model).update({_id: oid}, data, {safe: true, upsert: true}, function (

By manually adding the provided `id` to the data to insert, we make it possible to attain the database.