Mongo多个$ lookup和$ group合计

Pat*_*der 4 mongodb aggregation-framework

因此,我尝试在查询中“加入” 3个不同的mongo集合。所以我需要的是mongo聚合中的Multiple $lookup$groupStatement。

我的3个收藏集如下所示:

用户:(伪代码)

{
  _id,
  username: String,
  group: <some group._id>
}
Run Code Online (Sandbox Code Playgroud)

组:

{
  _id,
  name: String,
  _parent: <another group._id>,
}
Run Code Online (Sandbox Code Playgroud)

列表:(这些是由用户拥有的“ itemlists”):

{
  _id,
  name: String,
  userId: <some user._id>
}
Run Code Online (Sandbox Code Playgroud)

因此,我想做的是,给定一些组ID或null(对于没有父级的组-最高级别的组)-获得groups该组的全部内容,找到users这些组及其所有内容lists

所以最后我需要这样的东西:

[
  {
    _id: someGroupId,
    name: someGroupName,
    type: "group",
    users: [
      {
        _id: someUserId,
        name: someUserName,
        type: "user",
        lists: [
          ... and the same again for the lists (type: "list")
        ]
      },
      ... another user
    ]
  },
  ... another group
]
Run Code Online (Sandbox Code Playgroud)

希望你明白我的意思!

我现在拥有的(感谢Simon Tretter和大量研究)-请不要介意与Meteor无关的语法,您会明白:

Groups.aggregate([
    { $match: { _parent: groupId } },
    { $sort: { name: 1 } },
    {
        $lookup: {
            from: 'users',
            localField: '_id',
            foreignField: 'group',
            as: 'users'
        }
    },
    {
        $unwind: {
            path: "$users",
            preserveNullAndEmptyArrays: true
        }
    },
    {
        $lookup: {
            from: 'lists',
            localField: 'users._id',
            foreignField: 'userId',
            as: 'users.lists'
        }
    },
    {
        $unwind: {
            path: "$users.lists",
            preserveNullAndEmptyArrays: true
        }
    },
    {
        $match: {
            $or: [
                { "users.lists": { $exists: false } },
                { "users.lists.supplier_id": supplierId }
            ]
        }
    },
    { $sort: { "users.lists.name": 1 } },
    {
        $project: {
            "name": 1,
            "type": { $literal: 'group' },
            "users._id": 1,
            "users.name": { $ifNull: ["$users.profile.company.name", "$users.username"] },
            "users.type": { $literal: 'user' },
            "users.lists._id": 1,
            "users.lists.name": 1,
            "users.lists.item_count": 1,
            "users.lists.type": { $literal: 'list' }
        }
    },
    {
        $group: {
            _id: '$users._id',
            name: { $first: "$users.name" },
            type: { $first: "$users.type" },
            children: {
                $push: "$users.lists"
            }
        }
    },
    { $sort: { "users.name": 1 } },
    // until here I have one document per user, with their lists inside the "children" key - all good!
    // now I have to group the users inside their groups ...
    // NOT WORKING: returns completely wrong stuff
    { $group: {
      _id: '$_id',
      name: { $first: "$name" },
      type: { $first: "$type" },
      children: {
        $push: "$users"
      }
    } },
    { $sort: { name: 1 } }
]);
Run Code Online (Sandbox Code Playgroud)

我希望有人能使我走上正确的道路…… 我能找到的最好的类似stackoverflow问题对我没有太大帮助。

谢谢,最好,P

Pat*_*der 7

由于@Veeram的帮助和一点点的调整,我最终得到了以下工作代码:

Groups.aggregate([
    { $match: { _parent: groupId } },
    { $sort: { name: 1 } },
    {
        $lookup: {
            from: 'users',
            localField: '_id',
            foreignField: 'group',
            as: 'users'
        }
    },
    {
        $unwind: {
            path: "$users",
            preserveNullAndEmptyArrays: true
        }
    },
    {
        $lookup: {
            from: 'lists',
            localField: 'userId',
            foreignField: 'users._id',
            as: 'users.lists'
        }
    },
    {
        $unwind: {
            path: "$users.lists",
            preserveNullAndEmptyArrays: true
        }
    },
    {
        $match: {
            $or: [
                { "users.lists": { $exists: false } },
                { "users.lists.supplier_id": supplierId }
            ]
        }
    },
    { $sort: { "users.lists.name": 1 } },
    {
        $project: {
            "name": 1,
            "type": { $literal: 'group' },
            "users._id": 1,
            "users.name": { $ifNull: ["$users.profile.company.name", "$users.username"] },
            "users.type": { $literal: 'user' },
            "users.lists._id": 1,
            "users.lists.name": 1,
            "users.lists.item_count": 1,
            "users.lists.type": { $literal: 'list' }
        }
    },
    {
        $group: {
            _id: {
                _id: "$_id",
                name: "$name",
                type: "$type",
                user_id: "$users._id"
            },
            user_id: {
                $first: "$users._id"
            },
            name: {
                $first: "$users.name"
            },
            type: {
                $first: "$users.type"
            },
            children: {
                $push: "$users.lists"
            }
        }
    },
    { $sort: { name: 1 } },
    {
        $group: {
            _id: "$_id._id",
            name: {
                $first: "$_id.name"
            },
            type: {
                $first: "$_id.type"
            },
            children: {
                $push: {
                    _id: "$user_id",
                    name: "$name",
                    type: "$type",
                    children: "$children"
                }
            }
        }
    },
    { $sort: { name: 1 } }
]);
Run Code Online (Sandbox Code Playgroud)