1

I have a following document

{
"_id" : ObjectId("5d3fd27b363f482cf67300b5"),
"is_active" : true,
"is_delete" : false,
"email" : "[email protected]",
"phone" : "1234567890",
"agencies" : [ 
    {
        "employees" : [ 
            ObjectId("5d3aeac1ea162253845ac7f8"), 
            ObjectId("5d3aeac1ea162253845ac7f9")
        ],
        "company_role" : [ 
            {
                "employees" : [ 
                    ObjectId("5d3aeac1ea162253845ac7f8"), 
                    ObjectId("5d3aeac1ea162253845ac7f9")
                ],
                "company" : ObjectId("5d3fd27b363f482cf67300b7"),
                "role" : "administrator"
            }, 
            {
                "employees" : [ 
                    ObjectId("5d3aeac1ea162253845ac7f8"), 
                    ObjectId("5d3aeac1ea162253845ac7f9")
                ],
                "company" : ObjectId("5d3fd27b363f482cf67300b8"),
                "role" : "administrator"
            }, 
            {
                "employees" : [ 
                    ObjectId("5d3aeac1ea162253845ac7f8"), 
                    ObjectId("5d3aeac1ea162253845ac7f9")
                ],
                "company" : ObjectId("5d3fd27b363f482cf67300b9"),
                "role" : "administrator"
            }, 
            {
                "employees" : [ 
                    ObjectId("5d3aeac1ea162253845ac7f8"), 
                    ObjectId("5d3aeac1ea162253845ac7f9")
                ],
                "company" : ObjectId("5d3fd27b363f482cf67300ba"),
                "role" : "administrator"
            }
        ],
        "agency" : ObjectId("5d3fd27b363f482cf67300b6")
    }
],
"createdAt" : ISODate("2019-07-30T05:15:39.345Z"),
"updatedAt" : ISODate("2019-07-30T05:15:39.451Z"),
"__v" : 0
}

I want to update the employees array based on the condition of the _id and agencies.agency. if employees exists then not add or else add it

I have tried as follows but it doesn't work for me

modelName.findOneAndUpdate({ ' _id': ObjectId("5d3fd27b363f482cf67300b5"), 'agencies.agency': ObjectId("5d3fd27b363f482cf67300b6") }, { $addToSet: { "agencies.$.employees": ObjectId("5d3aeac1ea162253845ac7f1"), "agencies.$.company_role.employees": ObjectId("5d3aeac1ea162253845ac7f1") } },{new:true})

I am using mongoose to update record

2
  • How do you determine which company_role should be modifed ? Commented Aug 2, 2019 at 6:14
  • currently I want to update all the company role employees Commented Aug 2, 2019 at 6:15

1 Answer 1

1

Two useful update operators you can use: $[< identifier > ] and $[] :

modelName.findOneAndUpdate({ '_id': ObjectId("5d3fd27b363f482cf67300b5") },
    { 
        $addToSet: { 
            "agencies.$[agency].employees": ObjectId("5d3aeac1ea162253845ac7f1"),
            "agencies.$[agency].company_role.$[].employees": ObjectId("5d3aeac1ea162253845ac7f1"),
        } 
    },
    {
        arrayFilters: [ { "agency.agency": ObjectId("5d3fd27b363f482cf67300b6") } ]
    })
Sign up to request clarification or add additional context in comments.

3 Comments

I have now tried to add employee in specific company but not able to do it so can you please help me out with addition condition of company id 5d3fd27b363f482cf67300ba
@Nitin please open another question for clarity
it works for me I have added { "com.company": ObjectId("5d3fd27b363f482cf67300b9") }, in arrayFilters and change the condition $addToSet to "agencies.$[agency].company_role.$[com].employees": ObjectId("5d3aeac1ea162253845ac7f1")

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.