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

212
Views
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 answers
Answer question

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 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!