Don't know how to update an array element with mongoose

Don't know how to update an array element with mongoose
0

#1

Well it seems that as soon as I pass one hurdle I immediately run into another, much higher one. I’ve gotten almost everything working in the voting app, chart and all, but I can’t seem to update the value of my votes array in the database. Not only that, but I’m not sure as to which method I should use (find, findOne, findOneAndUpdate, or just update).
This is my votes model:

const pollSchema = new Schema({
    user: {
        name: String,
        question: String,
        choices: Array
    },
    date: Date,
    votes: Array
})

By default the array is filled with 0’s, and the elements correspond to the choices array.


#2

Don’t forget to use mongoose’s .save() method after updating the count in the choices array to make the update persist.

I ran into this exact problem on the Voting app too


#3

Thanks for the response! So on the server I tried the findOne method, updated the array element, basically element++, and then I called save on the entire object. But when I look at the database, none of the votes have been updated. Which mongoose method did you use?


#4

Wow I got it! I had to add markModified, and now apparently it’s working.