如何在 mongoDB 的嵌套数据中使用聚合 $lookup?

How to use aggregate $lookup in nested data from mongoDB?

我有 collections 这样的:

// 任务

[
  {_id: '123', _user: '345', _solutions: ['567', '678'] }
]

// 解决方案

[
  {  _id: '567', _task: '123', _user: '345' },
  {  _id: '678', _task: '123', _user: '345' }
]

// 用户

[
 { _id: '345', name: 'Tom' }
]

使用此代码:

await db
    .collection<Task>('tasks')
    .aggregate([
      { $match: { _id: id } },
      {
        $lookup: {
          from: 'solutions',
          // I guess here should be pipeline  
          localField: '_solutions',
          foreignField: '_id',
          as: '_solutions',
        },
      },
      { $lookup: { from: 'users', localField: '_user', foreignField: '_id', as: '_user' } },
    ])

我的结果如下:

task = {
  _id: 5e14e877fa42402079e38e44,
  _solutions: [
    {
      _id: 5e15022ccafcb4869c153e61,
      _task: 5e14e877fa42402079e38e44,
      _user: 5e007403fd4ca4f47df69913, <-- this should be userObject instead
    },
    {
      _id: 5e164f31cafcb4869c153e62,
      _task: 5e14e877fa42402079e38e44,
      _user: 5e007403fd4ca4f47df69913, <-- this should be userObject instead
    }
  ],
  _user: [
    {
      _id: 5e007403fd4ca4f47df69913,
      _solutions: [Array],
      _tasks: [Array],
    }
  ]
}

而且我不知道如何 $lookup 变成 _solutions._user - 所以我将有确切的用户 object.

而不是 objectId

您可以 运行 $lookup with custom pipeline 进行外部查找,而常规查找则供用户使用:

db.tasks.aggregate([
    {
        $match: {
            _id: "123"
        }
    },
    {
        $lookup: {
            from: "solutions",
            let: { solutions: "$_solutions" },
            pipeline: [
                { $match: { $expr: { $in: [ "$_id", "$$solutions" ] } } },
                {
                    $lookup: {
                        from: "users",
                        localField: "_user",
                        foreignField: "_id",
                        as: "_user"
                    }
                },
                { $unwind: "$_user" }
            ],
            as: "_solutions",      
        }    
    }  
])

Mongo Playground