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

263
Visualizações
How do I increment a Number value in Mongoose?

People of StackOverflow, I am burning with the question, how do I increment a
Number value in Mongoose? I have tried the code below, though it is not working. I am trying to increment a value by one on the submission of a form. Below is my code:

app.post('/like', function (req, res) {
    var id = req.body.id;
    var query = {_id: id};
    var post = Meme.findOne(query);
    Meme.findOneAndUpdate(post, post.likes: post.likes+1)
});

Thanks so much for your valuable help!

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

0

You can use $inc for this purpose.

Try this:

var id = req.body.id;
Meme.findOneAndUpdate({_id :id}, {$inc : {'post.likes' : 1}}).exec(...);

For more info on $inc, Please read MongoDB $inc documentation

over 4 years ago · Santiago Trujillo Relatório

0

With mongoose version 5 an additional option "useFindAndModify" is needed:

 // Make Mongoose use mongoDB's `findOneAndUpdate()`. Note that this option is `true`
 // by default, you need to set it to false.
 mongoose.set('useFindAndModify', false);


 Meme.findOneAndUpdate( {_id: res._id}, 
      {$inc : {'UID' : 1}}, 
      {new: true}, 
      function(err, response) { 
           // do something
      });

Mongoose Depreciation Doc

over 4 years ago · Santiago Trujillo Relatório

0

Meme.findOneAndUpdate({ _id: res._id }, { $inc: {'post.like': 1 } }, {new: true },function(err, response) {
 if (err) {
 callback(err);
} else {
 callback(response);
}

Hope this link help to find your solution

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