post: function(object, options) { options = options || {}; var id = options.id || object.id; //console.log("Post IDs option : ", options.id, object); if (!id) id = object.id = options.id = ObjectID.createPk().toJSON(); //console.log("Creating IDs : ", id); var self = this; return deep.wrapNodeAsynch(self.collection, "insert", [object]) .fail(function(err) { if (err.code == 11000) return deep.errors.Conflict("Mongo post failed conflict :", err); }) .done(function(obj) { obj = obj.ops; //console.log("_____________________________ MONGO POST : res ", obj.ops) if (obj) obj = obj[0]; if (obj) delete obj._id; else return deep.errors.NotFound(); return obj; }); },
collection.findOne(search, function(err, found){ if (err) return deferred.reject(err); if (found === null) { if (!object.id) object.id = ObjectID.createPk().toJSON(); collection.insert(object, function(err, obj){ if (err) return deferred.reject(err); // .insert() returns array, we need the first element obj = obj && obj[0]; if (obj) delete obj._id; deferred.resolve(obj.id); }); } else { deferred.reject(id + " exists, and can't be overwritten"); } });