Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Query nested array from document

Given the following document data in collection called ‘blah’…

[
{
    "_id" : ObjectId("60913f55987438922d5f0db6"),
    "procedureCode" : "code1",
    "description" : "Description 1",
    "coding" : [
        {
            "system" : "ABC",
            "code" : "L111"
        },
        {
            "system" : "DEFG",
            "code" : "S222"
        }
    ]
},
{
    "_id" : ObjectId("60913f55987438922d5f0dbc"),
    "procedureCode" : "code2",
    "description" : "Description 2",
    "coding" : [
        {
            "system" : "ABC",
            "code" : "L999"
        },
        {
            "system" : "DEFG",
            "code" : "X3333"
        }
    ]
}
]

What I want to get is all of the coding elements where system is ABC for all parents, and an array of codes like so.

[
{ "code": "L111" },
{ "code": "L999" },
]

If I use db.getCollection('blah').find({"coding.system": "ABC"}) I get the parent document with any child in the coding array of ICD.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If I use…

db.getCollection("blah")
    .find({ "coding.system": "ABC" })
    .projection({ "coding.code": 1 })

I do get the parent documents which have a child with a system of "ABC", but the coding for "DEFG" seems to come along for the ride too.

{
    "_id" : ObjectId("60913f55987438922d5f0db6"),
    "coding" : [
        {
            "code" : "L989"
        },
        {
            "code" : "S102"
        }
    ]
},
{
    "_id" : ObjectId("60913f55987438922d5f0dbc"),
    "coding" : [
        {
            "code" : "L989"
        },
        {
            "code" : "X382"
        }
    ]
}

I have also tried experimenting with:

 db.getCollection("blah").aggregate(
   { $unwind: "$coding" },
   { $match: { "system": "ICD" } }
 );

.. as per this page: mongoDB query to find the document in nested array
… but go no where fast with that approach. i.e. no records at all.

What query do I need, please, to achieve something like this..?

[
{ "code": "L111" },
{ "code": "L999" },
...
]

or even better, this..?

[
"L111",
"L999",
...
]

>Solution :

db.collection.aggregate([
  {
    $match: { "coding.system": "ABC" }
  },
  {
    $unwind: "$coding"
  },
  {
    $match: { "coding.system": "ABC" }
  },
  {
    $project: { code: "$coding.code" }
  }
])

mongoplayground


db.collection.aggregate([
  {
    $match: { "coding.system": "ABC" }
  },
  {
    $unwind: "$coding"
  },
  {
    $match: { "coding.system": "ABC" }
  },
  {
    $group: {
      _id: null,
      coding: { $push: "$coding.code" }
    }
  }
])

mongoplayground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading