Find/aggregate 来自另一个集合,基于其在 mongodb 中的 _id

Find/aggregate from another collection based on its _id in mongodb

PetTable {
  _id: ObjectId,
  bdTable: string,
  status: ObjectId(ref=Status)
}

Status {
    _id : ObjectId,
    statusCode : number, --> exple: (1, 2, ...7)
    petId : ObjectId(ref=PetTable),
}

我想找到 bdTable = "zzzz" 和状态 =5 的 PetTable 的所有对象;

我不知道该怎么做?

运行 具有 $lookup 管道阶段的聚合操作如下:

db.PetTable.aggregate([
  { $match: { bdTable: 'zzzz' } },
  { $lookup: {
    from: 'Status',
    let: { status_id: '$status' },
    pipeline: [
      { $match: {
        $expr: { 
          $and: [
            { $eq: [ 5, '$statusCode' ] },
            { $eq: [ '$_id', '$$status_id' ] }
          ] 
        }
      } }
    ],
    as: 'status'
  } },
  { $match: { 
    $expr: { $gt: [ { $size: '$status' }, 0] }
  } },
]);

Mongo Playground