1

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?

1 Answer 1

1

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

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

Comments

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.