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

MongoDB query: all documents that contain reference to a specific id

I am trying to extract some data from a legacy mongo db (v. 2.0.4). I have data that is structured like this:

{"_id": "1",
 "@graph": {"ma:isMemberOf": [{"@id": "524224b804743b02a4c23488",
                               "title": "IHum 350",
                               "transcript": "False"},
                              {"@id": "53cfd59404743bc3c9119adf",
                               "restrictor": "578e89ae04743b7b0816beff",
                               "title": "Spanish 339",
                               "transcript": "False"}],
            "ma:title": "Toy title 1"},
"_id": "2",
 "@graph": {"ma:isMemberOf": [{"@id": "524224b804743b02a4c23488",
                               "title": "IHum 350",
                               "transcript": "False"}],
            "ma:title": "Toy title 2"}}

…and I want to write a query that will find all of the documents that are members of a particular group (matching the @id field). For example, I want to be able to search for 524224b804743b02a4c23488 and receive documents 1 and 2. Or to search for 53cfd59404743bc3c9119adf and receive only document 1.

I have tried several things, but I can’t figure out how to query embedded objects.

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

>Solution :

db.collection.find({
  "@graph.ma:isMemberOf.@id": <your query>
})

Here is the Mongo playground for your reference.

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