This is my schema and model:
var Account = new Schema({
name: String,
votes: [{
resourceId: Number,
vote: Number
}]
});
var user = mongoose.model('user', Account);
This code updates items in the array:
var resourceId = 123;
var vote = 1;
var anotherId = 321;
user.update({'votes.resourceId': resourceId}, {'$set': {
'votes.$.vote': vote,
'votes.$.resourceId': anotherId
}}, function(err) {console.log(err)})
}
What changes should I make to the code so that when update operator can't match the condition, it creates new item in the array (I've tried adding options object -- {upsert: true}, but it didn't work)