Tuesday 15 June 2010

mongodb - Mongoose findOneAndUpdate Upsert _id null? -


I have a method that either creates or updates the document for policy. Searching and trying different techniques, like I came with a blank _id for my document. Using FindByIdAndUpdate has a similar effect.

I see a document inserted in the archive, but the _id field is empty:

  export.savePolicy = function (plcy, callback) {Console.log ('priority : '+ Plcy.priority) {var policy = try new policy (plcy); Var query = {_id: plcy._id}; // This null var can be updated = {name: plcy.name || Defaults.policy Defaults Name, long Description: plcy.longDescription || Defaults.policyDefaults.longDescription, short description: plcy.shortDescription || Defaults.policyDefaults.shortDescription, Priority: plcy.priority, colorHex: plcy.colorHex || Defaults.policyDefaults.colorHex, Settings: plcy.settings || [], Parent policy id: plcy.parentPolicyId || Null} policy.fundon and update (query, update, {upset: true}, function (fault, data) {callback (mistake, data);}); } Hold (e) {log. Terror ('Exception while trying to save policy:' + e.message); Callback (E, blank); }   

Can anyone be able to get an ADID when it can not be updated?

_id value, so if you do not want it to be used in new docs then you need to make sure The value should be null with a new ObjectID in the query :
  var query = {_id: plcy._id}; If (! Query._id) {query._id = new mongoose.mongo.ObjectID ();} // The rest remains the same .. .    

No comments:

Post a Comment