2

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)

manidos
  • 3,244
  • 4
  • 29
  • 65
  • Possible dupe of http://stackoverflow.com/questions/8871363/upsert-array-elements-matching-criteria-in-a-mongodb-document – JohnnyHK Feb 28 '16 at 15:40

0 Answers0