I have the following data structure:
_id: "1234567890",
groups: [
{
"userId" : "123",
"votes" : [
{
"userId" : "abc",
"vote" : false,
}
]
},
{
"userId" : "456",
"votes" : [
{
"userId" : "def",
"vote" : true,
}
]
}
]
How can I $addToSet
or $push
to a specific nested votes
array?
I can successfully $addToSet
to the votes
array if I do the following:
db.collection.update({ _id: "1234567890"}, {$addToSet: { "groups.0.votes": { //new object data }}
How can I do the update based on the userId
that I pass in? Something like:
db.collection.update({ _id: "1234567890"}, {$addToSet: { "groups.[dynamic userId].votes": { //new object data }}
Thanks.