I have a document with the following nested documents in array, each with their own _id.
{
"_id": {
"$oid": "65d46f1eaa08d32595abca1a"
},
"inventory": [
{
"_id": "cash",
"qty": 100
},
{
"_id": "gold",
"qty": 0
}
]
}
I want to update both the ‘qty’ values of cash and gold in one update, i.e I want to transfer 100 cash to gold. So the end result will be 0 cash, and 100 gold.
The closest I’ve come is being able to find one element by _id, and update that, but I can’t figure out how to update both elements in the array at once.
var filter = Builders<Player>.Filter
.Eq(r => r.Id, new ObjectId("65d46f1eaa08d32595abca1a"))
& Builders<Player>.Filter.ElemMatch(x => x.Inventory, Builders<PlayerInventoryItem>.Filter.Eq(x => x.Id, "cash"));
var update = Builders<Player>.Update.Set(x => x.Inventory.FirstMatchingElement().Qty, 500);
var updateResult = await _collectionHelper.Players(_mongoClient).UpdateOneAsync(filter, update);
Also if I could do this atomically without any contention from multiple reads/updates causing conflicts that would be great.
Thanks