0

I have these 2 documents in my 'resources' collection -

db.resources.insertMany([
{
Name: "Mark",
Gender: "M",
State: "VA",
TeamRoles: 
    [
     {
        Team: "A",
        Role: "Quality Advisor",
        Active: true
     },
     {
        Team: "B",
        Role: "Systems Analyst",
        Active: true
     }
     ]

},
{
Name: "Stacy",
Gender: "F",
State: "GA",
TeamRoles: 
    [
     {
        Team: "A",
        Role: "Systems Analyst",
        Active: true
     },
     {
        Team: "B",
        Role: "Developer",
        Active: true
     }
     ]
}])

Here, I want to update the role from 'Systems Analyst' to 'Business Analyst' for all the matching objects in the TeamRoles array across all the documents in the resources collection. Is there a way to achieve this using the Mongo DB query language (not JavaScript)?

1 Answer 1

1

Please try this :

db.collection.updateMany(
   {'TeamRoles.Role': "Systems Analyst"},
   { $set: { "TeamRoles.$[element].Role" : "Business Analyst" } },
   { arrayFilters: [ { "element.Role": "Systems Analyst" } ] }
)

Collection Data :

/* 1 */
{
    "_id" : ObjectId("5e2776c7dc791f82e7e1736d"),
    "Name" : "Mark",
    "Gender" : "M",
    "State" : "VA",
    "TeamRoles" : [ 
        {
            "Team" : "A",
            "Role" : "Quality Advisor",
            "Active" : true
        }, 
        {
            "Team" : "B",
            "Role" : "Systems Analyst",
            "Active" : true
        }, 
        {
            "Team" : "C",
            "Role" : "Systems Analyst",
            "Active" : true
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5e2776c7dc791f82e7e1736e"),
    "Name" : "Stacy",
    "Gender" : "F",
    "State" : "GA",
    "TeamRoles" : [ 
        {
            "Team" : "A",
            "Role" : "Systems Analyst",
            "Active" : true
        }, 
        {
            "Team" : "B",
            "Role" : "Developer",
            "Active" : true
        }
    ]
}

Result :

/* 1 */
{
    "_id" : ObjectId("5e2776c7dc791f82e7e1736d"),
    "Name" : "Mark",
    "Gender" : "M",
    "State" : "VA",
    "TeamRoles" : [ 
        {
            "Team" : "A",
            "Role" : "Quality Advisor",
            "Active" : true
        }, 
        {
            "Team" : "B",
            "Role" : "Business Analyst",
            "Active" : true
        }, 
        {
            "Team" : "C",
            "Role" : "Business Analyst",
            "Active" : true
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5e2776c7dc791f82e7e1736e"),
    "Name" : "Stacy",
    "Gender" : "F",
    "State" : "GA",
    "TeamRoles" : [ 
        {
            "Team" : "A",
            "Role" : "Business Analyst",
            "Active" : true
        }, 
        {
            "Team" : "B",
            "Role" : "Developer",
            "Active" : true
        }
    ]
}

Ref : updateMany-arrayFilters

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.