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

109
Views
How would I increment this number in mongoose?

I want to increment or decrement the likes value in the schema. My fear is that if two people were to grab the value of likes close to the same time, and they both incremented it, the value would only increment once.

For instance if likes = 100 and person A incremented it at the same time person B incremented it, likes would be 101 instead of 102.

Should I make a separate schema for likes and for each like I give it a postId?

var postSchema = mongoose.Schema({
    name: {
        type: String,
        required: true,
        unique: false
    },
    likes: {
         type: Number, 
         default: 0 
    }
});
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use this in mongoose to increment(change likes to -1 for decrement)

var query = {};
  var update = {$inc: {likes: 1}};
  var options = {'new': true};
  PostSchema.findOneAndUpdate(query, update, options, function (err, doc) {...
  })
over 4 years ago · Santiago Trujillo 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!