首页 > 解决方案 > $lookup 在我的节点 js 中不起作用,因为它在我的指南针测试中起作用

问题描述

所以我在我的 nodejs 应用程序上有一个聚合,它不是在查找引用的文档,但是当我在 compass 上手动测试它时,它会显示结果。所以也许我对猫鼬做错了什么。这是我的应用程序中的代码

 const notifications = await Notification.aggregate([
        {
            $match: {
                destinationUserId: userId
            }
        },
        {
            $lookup: {
                from: "rides",
                localField: "ride",
                foreignField: "_id",
                as: "rideInfo"
            }
        }
    ]).skip(offset).limit(limit).sort(`${order}${fieldForOrder}`);
    console.log({notifications});

当我这样做时,我会在 console.log 上得到这个结果

{
  notifications: [
    {
      _id: 5e9ca58c486ac8a1e0bc80e5,
      authorUserId: 'abfb258d-3ce8-40a4-a214-45afe7c71687',
      destinationUserId: '1ef488c7-9078-4378-9ac9-1e9f650048e5',
      title: 'Your ride is due',
      message: 'You should be on way to your destination.  You have 1 passengers. ',
      state: 'read',
      rideId: 5e9ca4d4486ac8a1e0bc80e4,
      __v: 0,
      createdAt: 2020-04-19T19:25:00.014Z,
      updatedAt: 2020-04-19T21:26:09.079Z,
      rideInfo: []
    }
  ]
}

所以我在rideInfo 中得到一个空数组。

当我在指南针上进行相同的查询时,我确实获得了有关乘车信息的信息 在此处输入图像描述

标签: node.jsmongodbmongoose

解决方案


推荐阅读