2

I want to update an array of strings which is nested inside an array of Objects.

This is my Mongo document:

{
    "_id" : ObjectId("5a52d995734d1d388d17eb0b"),
    "teams" : [ 
        {
            "assignedModels" : [ 
                "5a1665a82c7eb90001a7d676", 
                "58d8fc2d734d1d5af6dd4803"
            ]
        }, 
        {
            "assignedModels" : [ 
                "58d8fc2d734d1d5af6dd4803"
            ]
        }
    ]
}

Now i want to remove "58d8fc2d734d1d5af6dd4803" string from the assignedModels of each team object.

I have already tried some queries, but nothing is working as i expected

Current query:

db.collection('organisations').updateMany(
    { _id: database.ObjectID("5a52d995734d1d388d17eb0b") ,'teams.assignedModels' : { $exists:true } },
    { $pull : {'teams.$.assignedModels' : "58d8fc2d734d1d5af6dd4803" } },
    { multi: true });

Current output:

updated the 0th element of the teams array correctly but does not traverse through the other objects.

i tried $[], teams.assignedModels as well

Expected output:

{
    "_id" : ObjectId("5a52d995734d1d388d17eb0b"),
    "teams" : [ 
        {
            "assignedModels" : [ 
                "5a1665a82c7eb90001a7d676"
            ]
        }, 
        {
            "assignedModels" : [ 
            ]
        }
    ]
}
2
  • Which version of MongoDB are you using? Commented Jan 17, 2018 at 12:06
  • mongod version: 3.4.10 (MMAPv1) (Mlab) Commented Jan 17, 2018 at 12:13

1 Answer 1

1

In MongoDB 3.4, there is no way to do this nicely. You will need to either iterate over your elements (client-side) and update everything one-by-one. Or you could run the following query multiple times (until there are no more updates):

db.collection('organisations').updateMany(
    { _id: database.ObjectID("5a52d995734d1d388d17eb0b"), 'teams.assignedModels': "58d8fc2d734d1d5af6dd4803" },
    { $pull : {'teams.$.assignedModels' : "58d8fc2d734d1d5af6dd4803" } }
);

For MongoDB 3.6 you can simply do this:

db.collection('organisations').updateMany(
    { _id: database.ObjectID("5a52d995734d1d388d17eb0b") },
    { $pull : {'teams.$[].assignedModels' : "58d8fc2d734d1d5af6dd4803" } }
);

There's a catch, though, in case you're updating your MongoDB v3.4 to v3.6 which is documented here:

3.6 deployments have the following default featureCompatibilityVersion values: [...] For deployments upgraded from 3.4: "3.4" until you setFeatureCompatibilityVersion to "3.6".

So you will need to run the following command once to unleash the 3.6 power:

db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )
Sign up to request clarification or add additional context in comments.

2 Comments

"there is no way to do this nicely" - is this still true?
@ghosh: As I wrote above, for MongoDB 3.4 there is no nice way. From 3.6 onwards, this can be done nicely like I also described above.

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.