How can i update multiple documents in mongoose

0 votes

I found the following script:

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

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) {
  //...
});
Oct 15, 2020 in Node-js by kartik
• 37,490 points
490 views

1 answer to this question.

0 votes

Hello @kartik,

Use:

Device.updateMany({}, { cid: '' });

Hope it works!!

answered Oct 15, 2020 by Niroj
• 82,580 points

Related Questions In Node-js

0 votes
1 answer

How do I update devDependencies in NPM?

Hello @kartik, To update package.json in addition to ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,580 points
1,051 views
0 votes
1 answer

How can I get the browser language in node.js?

Hello @kartik, You can use req.headers["accept-language"] to get the language/locale ...READ MORE

answered Oct 16, 2020 in Node-js by Niroj
• 82,580 points
365 views
0 votes
1 answer

How do I define methods in a Mongoose model?

Hello @kartik, You didn't specify whether you were ...READ MORE

answered Nov 27, 2020 in Node-js by Niroj
• 82,580 points
57 views
0 votes
1 answer

How do I perform an id array query in Mongoose?

Hello @kartik, Here is a mongoosey way to ...READ MORE

answered Nov 29, 2020 in Node-js by Niroj
• 82,580 points
166 views
0 votes
1 answer

How to write files in Node.js?

Hello @kartik, Currently there are three ways to ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,580 points
144 views
0 votes
1 answer

How to Install a local module using npm?

Hello @kartik, This is what worked for me: npm ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,580 points
224 views
0 votes
1 answer

How to run Gulp tasks sequentially one after the other?

Hello @kartik, By default, gulp runs tasks simultaneously, ...READ MORE

answered Jul 12, 2020 in Node-js by Niroj
• 82,580 points
169 views
0 votes
1 answer

How to set environment variables from within package.json?

Hello @kartik, Set the environment variable in the ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,580 points
5,294 views
0 votes
1 answer

How can I update npm on Windows?

Hello @kartik, To update NPM, this worked for ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,580 points
2,000 views
0 votes
1 answer

How can i get the extension of the image in node.js?

Hello @kar You can do the following to ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,580 points
228 views