Hi, updateMany and updateOne accepts a limited aggregation pipeline.
We need to remove an item from an array (e.g via $pull) and update a property based on the result.
db.bills.insertMany([
{
_id: "111",
balance: 40000,
payments: [
{ billPaymentId: "123", amount: 25000 },
{ billPaymentId: "456", amount: 15000 } ],
},
{
_id: "222",
balance: 12,
payments: [
{ billPaymentId: "99", amount: 3 },
{ billPaymentId: "101", amount: 9 } ],
}
])
We are trying to remove some element from payments and update balance with the sum of the payments that are left.
If $pull would have worked with updateOne it would look like this:
db.bills.updateOne(
{ _id: "606f028d-9409-4f67-bd5d-f254c258ff0c" }, [
{ $pull: { billPaymentId: "427066e5-5a4a-4667-8f86-c10cb2560c77" } },
{ $set: { balance: { $sum: '$payments.amount' } } }
])
but as mentioned, $pull is not supported and this query return Unrecognized pipeline stage name: '$pull'.
Any other ideas maybe?