Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

230
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda