Below is my collection.
{
'_id': ObjectId('603fd4575250717d17aba89e'),
'user_id': 19,
'items': [
{
'price': 5,
'id': '342566b0d5f14117a228d8b17f215c4a'
},
{
'price': 5,
'id': '09e12c647fdf4c409e934eac23c73789'
}
],
}
I need to update the price for the element with the id 09e12c647fdf4c409e934eac23c73789, its current value is 5 and I need to update it to 6, may I know the query for the same please.
I tried with the below query and it did not work.
a = db.sample_collection.update_one({"items.$.id": "09e12c647fdf4c409e934eac23c73789"},
{"$set": {"items.$price": 6}})
Can someone help me on the correct query please.
The update query should look like this:
a = db.sample_collection.update_one({"items.id": "09e12c647fdf4c409e934eac23c73789"}, { "$set": { "items.$.price": 6 }})
This is an example of updating a document in an array. When performing an update, the positional operator, $ should only appear in the update document, not the query document.