Yashwanth Kumar K C
Yashwanth Kumar K C

Reputation: 1

Updating object value in Mongo DB nested Array

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"
    }
]
}

Upvotes: 0

Views: 85

Answers (1)

Talhakaanozkan
Talhakaanozkan

Reputation: 174

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.

Upvotes: 1

Related Questions