I hope someone can help.
I have inherited a project using an atlas mongo db. I has been asked to get data from a collection. Assume documents are structured in the collection like this:
{ _id: 'Parent1', kids: [ { _id: 'Test1', grandkids: [ { name: 'Blah' } ] } ] },
{ _id: 'Parent2', kids: [ { _id: 'Test1', grandkids: [ { name: 'Bloh' } ] } ] },
{ _id: 'Parent3', kids: [ { _id: 'Test2', grandkids: [ { name: 'Bloh' } ] } ] }
I need to get the grandkid with the name ‘Bloh’ when the kid’s _id is ‘Test 1’, which should return data from the 2nd document.
Using the Cloud MongoDb web ui, I tried querying like this, jsut to see if I could get the 2nd document back:
{
'$and': [
{ 'kids._id': ObjectId('Test 1') },
{ 'kids.grandkids.name': 'Bloh' }
]
}
But I get 0 results. I suspect it might be because the _id in the kids is not unique.
Assuming I can get the 2nd doucment back, how do I just get the matching grandkid back?
Thanks in advance!