简体   繁体   中英

Mongo query with inner select (in MongoDB Compass filter)

I would like to create a mongo query with level between my documents. I tried join or inner select without success. I use the filter of MongoDB Compass to query my results.

A parent document:

{
  'id':'parent01'
}

There are participation's documents who belong to a parent document:

{
  'participation:parent': 'parent01',
  'id':'participation01',
   ...
}

and observation's documents who belong to a participation:

{
  'obs:participation':'participation01',
  'id':'obs01'
}

I need to extract all documents who belong to the parent: the parent itself, all the participations and all the observations. I only have the parent id to do the query. It's really easy in SQL but I can't do it in Mongo (I use the filter of MongoDB Compass).

Thanks a lot for your help.

One answer below

[{
    $match: {
        'participation:parent': 'parent01'
    }
}, {
    $lookup: {
        from: 'default',
        localField: 'id',
        foreignField: 'obs:participation',
        as: 'myObs'
    }
}, {
    $unwind: {
        path: '$myObs',
        preserveNullAndEmptyArrays: false
    }
}, {
    $project: {
        myObs: 1
    }
}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM