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

218
Visualizações
How to get the original number I am incrementing in mongoose

I am working on a project, where when the user likes a comment nested inside comments array in a post document inside the posts collection, I use the following code to increment the number of likes on the comment:

await Post.findOneAndUpdate({ "postComments.commentId": commentId }, { $inc: { "postComments.$.commentLikes": 1 } });

The issue is, I need the original number I am incrementing to return it to the user view to increment the displayed number of likes dynamically, and I can't get the whole object because it will return the whole post, where the comment is nested inside the comments array and I don't know which index is that. How do I get the original number of likes from the nested comment?

Thank you in advance :)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

use projection in findOneAndUpdate

db.getCollection('comment').findOneAndUpdate(
{
  "postComments.commentId": "1"
},
{
  $inc: {
    "postComments.$.commentLikes": 1
  }
},
{ 
   projection: { postComments: { $elemMatch: { commentId: { $eq: "1" } } }}
})
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