1

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

3
  • arrayFilters are introduced in 3.6, Do you've multiple test2 element in applications array ? Kind of can it be duplicated ? or does that exist only once in applications array.. Commented Jun 10, 2020 at 18:47
  • Not Duplicates, User1 will also have application test2 and User2 will also have application test2 @whoami Commented Jun 11, 2020 at 8:28
  • actually my ask is can test2 be repeated in applications array for a document !! You can check my answer.. Commented Jun 11, 2020 at 17:55

1 Answer 1

1

As arrayFilters was only introduced after 3.6 you need to do it in two update operations, We can use .bulkWrite() to achieve this in one DB call.

Query :

db.collection.bulkWrite([
  /** First push `test2Updated` element to applications array where `test2` exists */
  {
    updateMany: {
      filter: { applications: "test2" },
      update: { $push: { applications: "test2Updated" } }
    }
  },
  /** Remove `test2` from applications */
  {
    updateMany: {
      filter: { applications: "test2" },
      update: { $pull: { applications: "test2" } }
    }
  }
]);

Note :

Since we can't use $set + $unset both on array field at same time we've split this into two ops.

Also if you've multiple test2 elements in same applications array use $pullAll to pull all of them out but you can't insert equivalent no.of test2Updated elements as you don't know how many test2's exist for a document in that case you need to individually read docs to code and update applications array(I guess this is not the case).

One other thing is test2Updated will not be pushed to same index at which test2 is but I guess this is not that important.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.