Home > front end >  MongoDB query: all documents that contain reference to a specific id
MongoDB query: all documents that contain reference to a specific id

Time:12-17

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.

CodePudding user response:

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

Here is the Mongo playground for your reference.

  • Related