I want to increment two values, one in high level document and one in embedded document:
{
studentId: "x1"
numberOfAttending: 2
courses: [
{
courseId:"y1"
numberOfAttending: 1
},
{
courseId:"y2"
numberOfAttending: 1
}
]
}
How could i inc the number of attending for student and for the course (upsert). and could i do it with a single update query ?
That’s going to be tough since
coursesis an array. You’ll need to know the index of the course you want to update, then do something like:Have you thought about switching it to a single embedded doc with
courseIdas a key for each course? If so, you can run a command like this to increment both. This doesn’t depend on position so it’s going to be less fragile: