Having a collections as below:
[{
"_id" : ObjectId("5ee111e541050ba2728adb3f"),
"User" : "user1",
"applications" : ["test1", "test2","test3"]
},
{
"_id" : ObjectId("5ee111e541050ba2728adb40"),
"User" : "user2",
"applications" : ["test1", "test3","test2"]
}]
Expected Output:
[{
"_id" : ObjectId("5ee111e541050ba2728adb3f"),
"User" : "user1",
"applications" : ["test1", "test2Updated","test3"]
},
{
"_id" : ObjectId("5ee111e541050ba2728adb40"),
"User" : "user2",
"applications" : ["test1", "test3","test2Updated"]
}]
Used Below Query:
db.getCollection('testUser').update({"applications":"test2"},
{$set:{"applications.$[elem]":"Test2Updated"}},
{ "arrayFilters": [{ "elem": "test2" }], "multi": true })
Not working in MongoDB 3.4 Any Solutions to make it work on MongoDB 3.4 could be helpful
arrayFiltersare introduced in3.6, Do you've multipletest2element inapplicationsarray ? Kind of can it be duplicated ? or does that exist only once inapplicationsarray..test2be repeated inapplicationsarray for a document !! You can check my answer..