0

i have a document which consists on nested array. i need to update nested array object value whose position is dynamic. i need to update locators:id value :'obj1' to value :'obj2'. But the problem is these locators position are not static they will be always dynamic

           {
       "_id" : ObjectId("5cf8fcac9f938484cb872ed2"),
       "projectId" : "pID92",
       "pageName" : "trail",
       "image" : "2e",
       "pageId" : "1",
       "objectName" : [ 
        {
        "objectName" : "demoPage",
        "attributes" : [ 
            {
                "locators" : "tagName",
                "value" : "p"
            }, 
            {
                "locators" : "className",
                "value" : "btnKeyEvent"
            }, 
            {
                "locators" : "id",
                "value" : "obj1"
            }, 
            {
                "locators" : "text",
                "value" : "Login"
            }
        ]

        "pomObject" : "newtrial1(driver).demo_1vij"
    }
]
}

i tried by hard coding the object position like this

    db.objectRepository.update({
    $and:[
    {pageName:'trail'},
    {'objectName.objectName':'demoPage'},
    {'objectName.attributes':{$elemMatch: {"locators": 'id', 
    "value": 'obj1'}}}
    ]},
     {"$set": { "objectName.0.attributes.2.value" : 'obj2'}}) 

please tell me how to solve this. Thank You

Expected out put

           {
       "_id" : ObjectId("5cf8fcac9f938484cb872ed2"),
       "projectId" : "pID92",
       "pageName" : "trail",
       "image" : "2e",
       "pageId" : "1",
       "objectName" : [ 
        {
        "objectName" : "demoPage",
        "attributes" : [ 
            {
                "locators" : "tagName",
                "value" : "p"
            }, 
            {
                "locators" : "className",
                "value" : "btnKeyEvent"
            }, 
            {
                "locators" : "id",
                "value" : "obj2"
            }, 
            {
                "locators" : "text",
                "value" : "Login"
            }
        ]

        "pomObject" : "newtrial1(driver).demo_1vij"
    }
]
}

1 Answer 1

1

I understand from the problem.

The location of obj1 is not fixed.

( 0 - 1 - 2 ) array order, you do not want to write.

db.objectRepository.update(
   { "objectName.0.attributes.value" : "obj1" },
   { $set : { "objectName.0.attributes.$.value" : "obj2222" }},
   {
     multi: false,
   }
)

query

result

"objectName.0." You can do the same in.


db.objectRepository.update(
    { },
    { $set : { "objectName.$[element].attributes.$[velement].value" : "aa change" } },
    { 
        multi: false,
        arrayFilters : [
            { "element" : { "objectName.objectName" : "demoPage" }},
            { "velement" : { "objectName.attributes.value" : "aa" }}        
        ]
    }
)

that's exactly what you want?

https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/

Even if this is not the answer to the question you want is definitely on this page.

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

3 Comments

Hi Talhakaanozkan, thanks for the reply. i need to match multiple condition to update my object like page and objectName should be matched. if i use $and to above query it will updating object at first position
Can you check ? Does it meet what you want now?
Hi, i cross check the query, its throwing an error,Error:"No array filter found for identifier 'element' in path 'objectName.$[element].attributes.$[velement].value'"

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.