node.js - Update an array of documents in Mongoose -
how can update documents in collection attributes value needs different (a unique number) each document?
below current code. seems update (i don't error) values in db not being updated.
model.find({}, function (err, docs) { if (err) { console.log(err); }; if(docs && docs.length > 0){ for(var i=0; i<docs.length; i++){ //set new value each doc - value must unique each doc docs[i].code = generaterandomcode(); } // pass in array of updated docs saved model.update(docs, function (err, docs) { if (err) { console.log(err); } if(!err){ req.updatedsuccessfully = true; } return next(); }); } else{ return next(); } }); before trying this:
model.update({}, { code: generaterandomcode() }, { multi: true }, function (err, numberaffected, raw) { if (err) return handleerror(err); console.log('the number of updated documents %d', numberaffected); console.log('the raw response mongo ', raw); }); the problem generaterandomcode() called once need create different code each document. neither of these example work.
instead of trying model.update(), can try save documents?
see answer question on url: update model mongoose, express, nodejs
Comments
Post a Comment