Home > database >  Unable to update data in mongoDB
Unable to update data in mongoDB

Time:01-16

I have Json structure like below

{
    "organizationId" : 339975,
    "domains" : [
        {       
        "application" : "ABC",
         "activeInd": true,
         "subdomain":[
                      {"url":"URL1",
                       "clientName":"ABCClient"
                      }
                        ]
         },
        {       
        "application" : "mno",
         "activeInd": false,
         "subdomain":[
                      {"url":"URL2",
                       "clientName":"MNOClient"
                      }
                     ]
         },
        {       
        "application" : "pqr",
         "activeInd": false,
         "subdomain":[
                      {"url":"URL3",
                       "clientName":"pqrClient"
                      }
                     ]
         },
        {       
        "application" : "egh",
         "activeInd": true,
         "subdomain":[
                      {"url":"URL4",
                       "clientName":"eghClient"
                      }
                     ]
         }]
}

when I tried to run the below query

var dataToUpdate =  "updatedUrl3"
db.orgConfigData.update(
    { "organizationId" : 339975,domains: { $elemMatch: { "application" : "pqr" } }},
            { $set: { 'domains.$[elemx].subdomain.url': dataToUpdate } },
            {"arrayFilters":[
                {
                    "elemx.url":"URL3"
                }]}
       
            )

I'm unable to update data in MongoDB, It gives results like below.

WriteResult({
    "nMatched" : 1,
    "nUpserted" : 0,
    "nModified" : 0
})

I understand that it is able to find the match but not updating the data, can someone help me to find an effective method to use it and get the output(get it updated).

I need the output like below once I run the query

{
    "organizationId" : 339975,
    "domains" : [
        {       
        "application" : "ABC",
         "activeInd": true,
         "subdomain":[
                      {"url":"URL1",
                       "clientName":"ABCClient"
                      }
                        ]
         },
        {       
        "application" : "mno",
         "activeInd": false,
         "subdomain":[
                      {"url":"URL2",
                       "clientName":"MNOClient"
                      }
                     ]
         },
        {       
        "application" : "pqr",
         "activeInd": false,
         "subdomain":[
                      {"url":"updatedUrl3",
                       "clientName":"pqrClient"
                      }
                     ]
         },
        {       
        "application" : "egh",
         "activeInd": true,
         "subdomain":[
                      {"url":"URL4",
                       "clientName":"eghClient"
                      }
                     ]
         }]
}

CodePudding user response:

Check this :

playground

example:

 db.orgConfigData.update({
   "organizationId": 339975
 },
 {
   $set: {
     "domains.$[elemx].subdomain.$[elemy].url": "updatedUrl3"
   }
    },
    {
  "arrayFilters": [
   {
  "elemx.application": "pqr"
   },
   {
    "elemy.url": "URL3"
   }
  ]
})

explained:

  1. Filter the organizationId: 339975 documents in the query part of update query
  2. Set the value only for the filtered elements located via the two arrayFilters defined as elemx & elemy
  •  Tags:  
  • Related