Home > Enterprise >  mongodb: updatemany against nested array objects
mongodb: updatemany against nested array objects

Time:01-24

Here my current document:

{
   "_id":"d283015f-91e9-4404-9202-093c28d6a931",
   "referencedGeneralPractitioner":[
      {
         "resourceType":"practitioner",
         "id": "id1",
         "cachedIdentifier":[
            {
               "system":{
                  "value":"urn:oid:1.3.6.1.4.1.19126.3"
               },
               "value":{
                  "value":"14277399B"
               }
            }
         ]
      }
   ]
}

It's only one document, but into this collection patient I have a lot of patients.

I need to update every document where:

  1. contains referencedGeneralPractitioner.id is "xxx", and
  2. update entire referencedGeneralPractitioner[matched_document] with incoming one.

For example, imagine I've bottom documents on patient collection:

{
   "_id":"d283015f-91e9-4404-9202-093c28d6a931",
   "referencedGeneralPractitioner":[
      {
         "resourceType":"practitioner",
         "id": "id1",
         "cachedIdentifier":[
            {
               "system":{
                  "value":"urn:oid:1.3.6.1.4.1.19126.3"
               },
               "value":{
                  "value":"14277399B"
               }
            },
            {
               "system":{
                  "value":"urn:oid:1.3.6.1.4.1.19126.3"
               },
               "value":{
                  "value":"43756837R"
               }
            }
         ]
      }
   ]
},
{
   "_id":"df342343-45d5-cf4a-8374-17dc436d40ca",
   "referencedGeneralPractitioner":[
      {
         "resourceType":"practitioner",
         "id": "id1",
         "cachedIdentifier":[
            {
               "system":{
                  "value":"urn:oid:1.3.6.1.4.1.19126.3"
               },
               "value":{
                  "value":"14277399B"
               }
            },
            {
               "system":{
                  "value":"urn:oid:1.3.6.1.4.1.19126.3"
               },
               "value":{
                  "value":"43756837R"
               }
            }
         ]
      },
      {
         "resourceType":"practitioner",
         "id": "id2",
         "cachedIdentifier":[
            {
               "system":{
                  "value":"urn:oid:1.3.6.1.4.1.19126.3"
               },
               "value":{
                  "value":"48596705T"
               }
            }
         ]
      }
   ]
}

I need to db.patient.referenceGeneralPractitioner[where id="id1] = {incoming updated referencedGeneralPractitioner}.

Imagine incoming updated refenrencedGeneralPractitioner} is:

{
   "resourceType":"practitioner",
   "id":"id1",
   "cachedIdentifier":[
      {
         "system":{
            "value":"urn:oid:1.3.6.1.4.1.19126.3"
         },
         "value":{
            "value":"INCOMING"
         }
      }
   ]
}

After that, I need to patient collection:

{
   "_id":"d283015f-91e9-4404-9202-093c28d6a931",
   "referencedGeneralPractitioner":[
      {
         "resourceType":"practitioner",
         "id":"id1",
         "cachedIdentifier":[
            {
               "system":{
                  "value":"urn:oid:1.3.6.1.4.1.19126.3"
               },
               "value":{
                  "value":"INCOMING"
               }
            }
         ]
      }
   ]
},
{
   "_id":"df342343-45d5-cf4a-8374-17dc436d40ca",
   "referencedGeneralPractitioner":[
      {
         "resourceType":"practitioner",
         "id":"id1",
         "cachedIdentifier":[
            {
               "system":{
                  "value":"urn:oid:1.3.6.1.4.1.19126.3"
               },
               "value":{
                  "value":"INCOMING"
               }
            }
         ]
      },
      {
         "resourceType":"practitioner",
         "id":"id2",
         "cachedIdentifier":[
            {
               "system":{
                  "value":"urn:oid:1.3.6.1.4.1.19126.3"
               },
               "value":{
                  "value":"48596705T"
               }
            }
         ]
      }
   ]
}

Any ideas?

CodePudding user response:

simple $update wtih arrayFilters

db.collection.update({
  "referencedGeneralPractitioner.id": "id1"
},
{
  $set: {
    "referencedGeneralPractitioner.$[elem]": {
      "resourceType": "practitioner",
      "id": "id1",
      "cachedIdentifier": [
        {
          "system": {
            "value": "urn:oid:1.3.6.1.4.1.19126.3"
          },
          "value": {
            "value": "INCOMING"
          }
        }
      ]
    }
  }
},
{
  "multi": true,
  "arrayFilters": [
    {
      "elem.id": {
        $eq: "id1"
      }
    }
  ]
})

mongoplayground

  •  Tags:  
  • Related