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,520 points
3,851 views

1 answer to this question.

0 votes

Hello @kartik,

Use:

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

Hope it works!!

answered Oct 15, 2020 by Niroj
• 82,840 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 17, 2020 in Node-js by Niroj
• 82,840 points
7,592 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,840 points
4,321 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,840 points
2,753 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 30, 2020 in Node-js by Niroj
• 82,840 points
6,262 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,840 points
928 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,840 points
9,866 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,840 points
1,515 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,840 points
23,988 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,840 points
6,465 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,840 points
2,124 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP