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
}
});
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) {...
})