Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

346
Vistas
MongoDB - Querying inner object that is an array

I am new to MongoDB queries and looking for some guidance on how to retrieve documents that fit the search criteria. In particular, I need to find all the id(s) of the documents where the code is JPID i.e, "code": "JPID". We can assume the name of the collection is systems.

Since this is a nested object I thought of using the $unwid - but I am still stuck on how to do it. The example is very trivial. Any help and guidance is appreciated.

{
      "resourceType": "NamingSystem",
      "id": "example-id",
      "name": "Austalian Healthcare Identifier - Individual",
      "status": "active",
      "kind": "identifier",
      "date": "2015-08-31",
      "publisher": "HL7 Australia on behalf of NEHTA",
      "responsible": "HI Service Operator / NEHTA",
      "type": {
        "coding": [
          {
            "system": "http://hl7.org/fhir/v2/0203",
            "code": "JPID";
            "display": "National unique individual identifier"
          }
        ],
        "text": "IHI"
      },
      "description": "Australian HI Identifier as established by relevant regulations etc",
      "uniqueId": [
        {
          "type": "oid",
          "value": "1.2.36.1.2001.1003.0",
          "comment": "This value is used in Australian CDA documents"
        },
        {
          "type": "uri",
          "value": "http://ns.electronichealth.net.au/id/hi/ihi/1.0",
          "preferred": true,
          "period": {
            "start": "2015-08-21"
          }
        }
      ]
    }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

  1. You can find all documents that contain at least one element in type.coding array by simple match the code element with the value JPID:

    db.system.find({
     "type.coding.code": "JPID"
    })
    

It is recommended to create and index on {"type.coding.code":1} so your searches to perform faster on this field.

playground

  1. If you need to find all documents and filter only the array entries having the code:"JPID" you can use $filter as follow:

    db.system.aggregate([
    {
     $match: {
       "type.coding.code": "JPID"
     }
    },
    {
     $addFields: {
       "type.coding": {
         "$filter": {
          "input": "$type.coding",
          "as": "tc",
          "cond": {
            $eq: [
              "$$tc.code",
              "JPID"
              ]
           }
         }
       }
     }
    }
    ])
    

playground(filter)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda