I am trying to update an array element inside a document without changing whole array.
Array elements look like this
Suppose i have to only update index 1
element's value. For that i have:
_id
of the document
index 1
's value ("optionalImages-624476a7bd4d2bfe6bf86e9a-1-1650025533684.jpeg")
to be updated value ("optionalImages-624476a7bd4d2bfe6bf86e9a-1-1650025534589.jpeg").
I think it can be updated by mongodb's arrayfilters
but i don't get the documentation correctly.
Your help will be highly appreciated.
Query1
$[m]
inside the path to specify the member value that we want to changeupdate(
{"_id": {"$eq": 1}},
{"$set": {"ar.$[m]": 100}},
{"arrayFilters": [{"m": {"$eq": 20}}])
Query2
update(
{"_id": {"$eq": 1}},
[{"$set":
{"ar":
{"$map":
{"input": "$ar",
"in": {"$cond": [{"$eq": ["$$this", 20]}, 100, "$$this"]}}}}}])