Nested elemMatch

Currently, that’s my current document:

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

Here, there’s two nested objects arrays: referencedGeneralPractitioner[{cachedIdentifier[{}]}].

Currently, I’m getting results using this query:

{
   "referencedGeneralPractitioner":{
      "$elemMatch":{
         "cachedIdentifier.value.value":"14277399B",
         "cachedIdentifier.system.value":"urn:oid:1.3.6.1.4.1.19126.3"
      }
   }
}

It’s getting my desired document, but I don’t quite figure out if above query is which I’m really looking for.

I mean, I’m only applying $elemMatch on referencedGeneralPractitioner field array.

  1. Is it really enought?
  2. Should I add a nested $elemMatch on cachedIdentifier?

Any ideas?

I do not think it is enough.

I created an extra document to demonstrate:

{
    _id: 'test-id-1',
    referencedGeneralPractitioner: [
      {
        resourceType: 'practitioner',
        cachedIdentifier: [
          {
            system: { value: 'urn:oid:1.3.6.1.4.1.19126.3' },
            value: { value: 'test-1' }
          },
          {
            system: { value: 'test-2' },
            value: { value: '14277399B' }
          }
        ]
      }
    ]
  }

If I use your query both documents are found. I am not sure that it is what you want. If you change the query to

{ "referencedGeneralPractitioner.cachedIdentifier":
  { "$elemMatch":
    { "value.value": "14277399B", "system.value": "urn:oid:1.3.6.1.4.1.19126.3" }
  }
}

then only your original document is found because both value.value and system.value matches within the same cachedItentifier.

So both are good but leads different results. It depends of your use-case which one is okay for your.