使用猫鼬查找填充一个集合

Populate one collection with lookup in mongoose

我正在尝试使用查找来填充集合。问题是我需要在 express js 中使用 mongoose 一次加入三个集合。 我有三个集合,即 users、skills、userskills

User 和 UserSkills 已连接。技能和 UserSkills 是相关联的。但不是用户和技能。

我的模型是这样的

用户

{ 
    _id: 5ec6d940b98e8f2c3cea5f22, 
    name: "test one", 
    email: "test@example.com" 
}

技能

{ 
    _id: 5ec786b21cea7d8c8c186a54, 
    title: "java" 
}

用户技能

{
    _id: 5ec7879c1cea7d8c8c186a56,
    skillId: "5ec786b21cea7d8c8c186a54",
    userId: "5ec6d940b98e8f2c3cea5f22", 
    level: "good" 
}

我试过了

user = await User.aggregate([{
        $lookup: {
          from: "userskills",
          localField: "_id",
          foreignField: "userId",
          as: "userskills"
        }
      }, {
        $unwind: {
          path: "$userskills",
          preserveNullAndEmptyArrays: true
        }
      }, {
        $lookup: {
          from: "skills",
          localField: "userskills.skillId",
          foreignField: "_id",
          as: "userskills.skill",
        }
      }, {
        $group: {
          _id : "$_id",
          name: { $first: "$name" },
          skill: { $push: "$skills" }
        }
      }, {
        $project: {
          _id: 1,
          name: 1,
          skills: {
            $filter: { input: "$skills", as: "a", cond: { $ifNull: ["$$a._id", false] } }
          } 
        }
      }]);

要求的结果:

{
    "users" : [
        {
            _id: "5ec6d940b98e8f2c3cea5f22"
            name: "test one",
            email: "testone@example.com",
            "skills" : [
                {
                    _id: "5ec7879c1cea7d8c8c186a56",
                    level: "good",
                    "skill" : {
                         _id: "5ec786b21cea7d8c8c186a54",
                         title: "java"
                    }
                }
           ]
        },
        {
            _id: "5ec6d940b98e8f2c3cea5f23"
            name: "test two",
            email: "testtwo@example.com",
            "skills" : [
                {
                    _id: "5ec7879c1cea7d8c8c186a57",
                    level: "good",
                    "skill" : {
                         _id: "5ec786b21cea7d8c8c186a55",
                         title: "php"
                    }
                }
           ]
        }
    ]
}

使用时

user = await User.find().populate('Skills").exec()

结果是这样的

{
    "users" : [
        {
            _id: "5ec6d940b98e8f2c3cea5f22"
            name: "test one",
            email: "testone@example.com",
            "skills" : [
                {
                    _id: "5ec7879c1cea7d8c8c186a56",
                    level: "good",
                    skillId: "5ec786b21cea7d8c8c186a55" 
                }
           ]
        }
    ]
}

问题是我还需要获取技能名称。请帮我解决这个问题。我正在用 nodejs 和 mongodb.

编写后端 API

您可以将第二个 $lookup 作为 custom pipeline 的一部分嵌入:

await User.aggregate([
    {
        $lookup: {
            from: "userskills",
            let: { user_id: "$_id" },
            pipeline: [
                { $match: { $expr: { $eq: [ "$$user_id", "$userId" ] } } },
                {
                    $lookup: {
                        from: "skills",
                        localField: "skillId",
                        foreignField: "_id",
                        as: "skill"
                    }
                },
                { $unwind: "$skill" }
            ],
            as: "skills"
        }
    }
])

Mongo Playground