MongoDB查找-使用$lookup

nr7wwzry  于 2022-11-22  发布在  Go
关注(0)|答案(2)|浏览(167)

因此,我为用户准备了JSON格式文档,其中包含以下结构:

[
  {
    "_id": {
      "$oid": "6369aeb83ce0f8168520f42f"
    },
    "fullname": "Jokona",
    "password": "$2b$10$MUAe7XIc/xtJTGVh/y1DeuShCARbwxCSejUbHaqIPZfjekNrn0.Yy",
    "NIK": "MT220047",
    "status": "active",
    "department": "Logistic",
    "position": "Management Trainee",
    "Group_Shift": "Non Shift",
    "role": "admin",
    "createdAt": 1667870392,
    "updatedAt": 1668564835,
    "__v": 0
  },
  {
    "_id": {
      "$oid": "6369b17b11e02557349d8de5"
    },
    "fullname": "Warana",
    "password": "$2b$10$0xaqz5V8bar/osWmsCiofet5bY10.ORn8Vme3QC7Dh0HwLHwYOm3a",
    "NIK": "17000691",
    "status": "active",
    "department": "Production",
    "position": "Foreman",
    "Group_Shift": "R1",
    "role": "user",
    "__v": 0,
    "createdAt": 1667871099,
    "updatedAt": 1668496775
  },
  
]

它尝试使用mongodb $lookup来查找,通过使用NIK作为foreignnkey加入来获取全名,下面是我所尝试的:

const dataAnaylitics = await Answer.aggregate([
        // $match stage
        {
          $group: {
            _id: {
              username: "$username",
              title: "$title",
              date: "$date",
            },
            count: {
              $sum: 1,
            },
            position: {
              $first: "$position",
            },
            department: {
              $first: "$department",
            },
          },
        },
        {
          $lookup: {
            from: "users",
            localField: "username",
            foreignField: "NIK",
            as: "fullname",
            pipeline: [{ $project: { fullname: 0 } }],
          },
        },
        {
          $group: {
            _id: {
              username: "$_id.username",
              title: "$_id.title",
            },
            dates: {
              $push: {
                k: "$_id.date",
                v: "$count",
              },
            },
            position: {
              $first: "$position",
            },
            department: {
              $first: "$department",
            },
          },
        },
        {
          $project: {
            _id: 0,
            username: "$_id.username",
            title: "$_id.title",
            position: 1,
            department: 1,
            dates: 1,
          },
        },
        {
          $replaceRoot: {
            newRoot: {
              $mergeObjects: [
                "$$ROOT",
                {
                  $arrayToObject: "$dates",
                },
              ],
            },
          },
        },
        {
          $unset: "dates",
        },
      ]);

但是结果没有返回fullname字段,是不是我的代码有什么问题?我查找了文档,并且已经按照步骤进行了操作

ntjbwcob

ntjbwcob1#

在分组阶段,由于您是根据用户名进行分组的,因此生成的文档将以_id.username作为字段。请在查找中将此字段用作localField。

{
          $lookup: {
            from: "users",
            localField: "_id.username",
            foreignField: "NIK",
            as: "fullname",
            pipeline: [{ $project: { fullname: 0 } }],
          }
jvlzgdj9

jvlzgdj92#

我已经把它修好了,希望它能帮助其他人。

const dataAnaylitics = await Answer.aggregate([
        // $match stage
        {
          $group: {
            _id: {
              username: "$username",
              title: "$title",
              date: "$date",
            },
            count: {
              $sum: 1,
            },
            position: {
              $first: "$position",
            },
            department: {
              $first: "$department",
            },
          },
        },
        {
          $lookup: {
            from: "users",
            localField: "_id.username",
            foreignField: "NIK",
            as: "fullname",
            pipeline: [{ $project: { _id: 0, fullname: 1 } }],
          },
        },
        {
          $group: {
            _id: {
              username: "$_id.username",
              title: "$_id.title",
            },
            dates: {
              $push: {
                k: "$_id.date",
                v: "$count",
              },
            },
            position: {
              $first: "$position",
            },
            department: {
              $first: "$department",
            },
            fullname: {
              $first: { $arrayElemAt: ["$fullname.fullname", 0] },
            },
          },
        },
        {
          $project: {
            _id: 0,
            username: "$_id.username",
            title: "$_id.title",
            position: 1,
            department: 1,
            dates: 1,
            fullname: 1,
          },
        },
        {
          $replaceRoot: {
            newRoot: {
              $mergeObjects: [
                "$$ROOT",
                {
                  $arrayToObject: "$dates",
                },
              ],
            },
          },
        },
        {
          $unset: "dates",
        },
      ]);

相关问题