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

269
Visualizações
how to prevent updating of "updatedAt" field while saving or updating documents in mongodb?

I am in a situation where i want to update documents of collection in mongodb but i don't want "updatedAt" field to be updated. I want to retain the previous value of updated field. Please help.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

If you are using Waterline or other database abstractions, my advice is to run the query natively in mongo.

This is how I would fix this in Sails.js (running Waterline):

let someQuery = {name: 'John Doe'}; 

MyModel.native(function (err, collection){
  collection.update(
    {query: someQuery},                   // Which documents to update?
    {$set:{field1: "value", foo:"bar"}},  // Our new values
    {multi: true},                        // To support update of multiple fields
    function(err, result){
      if (err) console.log(err)
      else console.log(result.result)
    })
});

over 4 years ago · Santiago Trujillo Relatório

0

updatedAt field is updated when you use update query of mongoose. if we directly use $set query it will only update the mentioned fields inside $ set query. The reason is updatedAt field is managed by mongoose not by mongodb.

over 4 years ago · Santiago Trujillo Relatório

0

If you are using mongoose, then relatively new feature (2020) gives you the option to override setting timestamps (or just one) on transactions.

Refer to docs

Example from docs shown below

const userSchema = mongoose.Schema({
  email: String
}, { timestamps: true });

const User = mongoose.model('User', userSchema);

const doc = await User.findOneAndUpdate({email: 'test@google.com'}, {email:'newtest@google.com'}, 
{new:true, upsert: true, timestamps:false});
over 4 years ago · Santiago Trujillo 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