I've got a simple poll trying to update the vote
counter every time someone upvte it.
I tried using $inc
but it has not effect. therefore it does return which supposed to be the updated poll/ poll after the vote
counter is updated, but it just returns the same one without increasing anything at all.
What am i doing wrong?
app.patch('/voting/:id', (req, res) => {
let userVote = req.body.votes;
Poll_Schema_Model.findByIdAndUpdate({ "_id": '5b070f512a28d70eb0abaa51' }, { $inc: { "poll[0].votes":userVote } }, { new: true }, (err, newPoll) => {
res.status(200).send(newPoll);
})
.catch(() => {
res.status(400).send();
});
});
the newPoll
results in :- (note that votes is defaulted to 0
)
{
"_id": "5b070f512a28d70eb0abaa51",
"_creator": "5b04aba0ee81bb26182b2267",
"poll": [
{
"votes": 0,
"_id": "5b070f512a28d70eb0abaa52",
"option1": "FIRST OPTIONNN",
"option2": "SECOND OPTIONN"
}
],
"__v": 0
}
My schema
:-
const Poll_Schema = new mongoose.Schema({
_creator: {
type: mongoose.Schema.Types.ObjectId
},
poll: [{
option1: {
type: String,
maxlength: 20,
minlength: 3
},
option2: {
type: String,
maxlength: 20,
minlength: 3
},
votes: {
type: Number,
minlength: 1,
default:0
}
}]
});