I have following user collection document:
user: {
_id: ObjectId("...")
name: "Kid",
items: [
{
name: "pencil",
type: "SCHOOL",
amount: 1.00
},
{
name: "computer",
type: "PERSONAL",
amount: 9999.99
},
{
name: "notebook",
type: "SCHOOL",
amount: 9.00
}
]
}
Now, I am trying to add a new field named schoolAmount to the top level document by summing amount in the subdocument that is of SCHOOL type. So in the above example document would haveschoolAmount: 10.00 // (1 + 9). How could I make such a query? I am only aware of simple query like below using the shell.
db.user.updateMany({}, {$set: {"schoolAmount": <I need help here>}});