pkyeck pkyeck - 5 months ago 23
Node.js Question

how can i update multiple documents in mongoose

I found the following script:

Device.find(function(err, devices) {
devices.forEach(function(device) {
device.cid = '';;

MongoDB has the "multi" flag for an update over multiple documents but I wasn't able to get this working with mongoose. Is this not yet supported or am I doing something wrong?!

Device.update({}, {cid: ''}, false, true, function (err) {


Currently I believe that update() in Mongoose has some problems, see: and

However, check the docs for update: (its under Model). The definition is:

Model.update = function (query, doc, options, callback) { ... }

You need to pass the options inside an object, so your code would be:

Model.update = function ({}, {cid: ''}, {multi: true}, function(err) { ... });

I believe that Mongoose wraps your cid in a $set, so this is not the same as running that same update in the mongo shell. If you ran that in the shell then all documents would be replaced by one with a single cid: ''.