Home > Enterprise >  UpdateMany syntax with MongoDB and Node.js
UpdateMany syntax with MongoDB and Node.js

Time:07-08

I can't understand why this code is not changing the database field for all records from "leavetakings" to "TheHike". The database calls and console.logs before and after the statement execute but this appears to do nothing yet causes no errors.

Player.updateMany({location: "leavetakings"},{location: "TheHike"});

I have tried using the $set syntax and async/await functions. Could someone please tell me what silly thing I am doing wrong?

CodePudding user response:

You need to use an atomic operator (such as $set)

The following should work correctly:

Player.updateMany({ location: "leavetakings" }, { $set: { location: "TheHike" } });

Use async/await or a promise to make sure that the query is being executed before the program exits.

  • Related