node.js - MongoDB/Mongoose: updating two different fields in Mongoose model -
is possible this:
var updatedata = { "$set": { content: 'foo' }, "$push":{ versions: { some: 'bar' } } }; model.update({},updatedata, {});
or need 2 or more separate updates?
there 1 update of course , mongoose has nothing it, , passes through operation directly. reading documentation $set
should shed bit more light on in general.
your syntax bit off , should be:
model.update({},updatedata,function(err,numaffected) { });
and of course if wanted affect multiple documents , not first match ( default here ), pass in "multi":
model.update({},updatedata,{ "multi": true },function(err,numaffected) { });
the suggestion of .find()
, .save()
shown not happens here , wrong way this. operation sequence means "two" operations touch database, , importantly possible data change in between .find()
operation , changes committed in .save()
can overwrite data.
operators $set
.update()
, related methods processed atomically. there 1 contact database , point of such operators change data "in place" without needing return client modification.
one true statement in comments turn of debugging:
mongooose.set("debug",true)
and see actual statements passed through driver mongodb.
it's 1 operation, , depending on arguments supplied can of course affect multiple fields , multiple documents.
Comments
Post a Comment