Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
Mongoose collection.update does not update the document

I am having an issue using Mongoose's update function. Basically the document I am trying to modify is not getting updated.

Here is the code:

  const user = 'Joe Bloggs'
  const salt = await bcrypt.genSalt()
  const pwStr = 'simplepassword'
  const hashedPassword = await bcrypt.hash(pwStr, salt)

  User.update({user_name: user }, { $set: { password: hashedPassword }}, function(err, doc) {
    if (err) console.log('Error ', err);
    console.log('Updated Doc -> ', doc); // returns un-updated doc
  });

As you can see there is not much to it. The only thing I thought could be causing an issue was the bcrypt functions, but they seem to be working and hashedPassword logs out fine.

The callback logs the document, but it is not updated and when I check it in the Mongo shell it is indeed not updated.

I previously tried findOneAndUpdate but it appears that has been deprecated.

So, I tried findOne, but this also failed to update the document. Here is the basic code which uses save on the found user instead.

User.findOne({user_name: user}).then(async function(user) {
      user.password = 'easypassword';
      await user.save();
    } 
});

I tried using update in the shell using the same { $set: {...}} syntax and it works.

If anyone can tell me why this operation isn't working when I try to do it using the Mongoose functions I'd much appreciate it. Thanks in advance!

EDIT

I have tried the suggested code below:

const res = await User.updateOne([filter], [query]); 

This returns the following when res is logged out:

{ acknowledged: false }

This appears in MongoDB documentation to relate to a "write concern" setting, but I have no idea where to go with it from there.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

update is what is actually deprecated.

findOneAndUpdate, like other mongoose update methods, it returns the un-updated object by default. Setting the option new to true will return the updated doc.

Another option would updateOne;

  User.findOneAndUpdate(
    { user_name: user },
    { $set: { password: hashedPassword } },
    { new: true },
    function (err, doc) {
      if (err) console.log("Error ", err);
      console.log("Updated Doc -> ", doc);  
    }
  );
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!