从 MongoDB 聚合中的 lookup 操作获取一个布尔值(仅限于单个值而非数组或对象)。

3

我正在尝试编写一个函数,返回用户是否为收藏夹中的用户。

我正在使用MongoDB聚合User模型,与收藏夹模型(其中保存了所有喜爱的用户)配合使用管道和$project进行查找。

以下是MongoDB聚合函数查询:

await User.aggregate([
            {
                $match: {
                    _id: ObjectId(_id),
                },
            },
            {
                $lookup: {
                    from: 'userportfolios',
                    localField: '_id',
                    foreignField: 'user',
                    as: 'user_portfolios',
                },
            },
            {
                $lookup: {
                    from: 'favourites',
                    pipeline: [
                        {
                            $match: {
                                user: ObjectId(data.user._id),
                                favourites: {
                                    $elemMatch: {
                                        $eq: ObjectId(_id),
                                    },
                                },
                            },
                        },
                        {
                            $project: {
                                _id: 0,
                                is_favourite: {
                                    $cond: [
                                        { $ifNull: ['$is_favourite', false] },
                                        { $literal: true },
                                        { $literal: false },
                                    ],
                                },
                            },
                        },
                    ],
                    as: 'is_favourite',
                },
            },
        ]);

我得到了这个结果

 [
    {
      "_id": "602b5078e8eb5339e0134351",
      "is_active": true,
      "is_admin": false,
      "is_block": false,
      "user_type": "company",
      "firstName": "Sagar",
      "lastName": "Davara",
      "mobileNumber": "*******",
      "email": "s*********@gmail.com",
      "password": "b7370338e54b1cb051be0dd54eba2f7f",
      "verificationCode": "210768",
      "createdAt": "2021-02-16T04:56:24.091Z",
      "updatedAt": "2021-02-16T04:56:24.091Z",
      "user_portfolios": [],
      "is_favourite": [
        {
          "is_favourite": false
        }
      ]
    }
  ]

但我想要得到 is_favourite: true 而不是一个对象数组,我需要从MongoDB中获取这个输出。

 [
    {
      "_id": "602b5078e8eb5339e0134351",
      "is_active": true,
      "is_admin": false,
      "is_block": false,
      "user_type": "company",
      "firstName": "Sagar",
      "lastName": "Davara",
      "mobileNumber": "6353764283",
      "email": "sagardspeed3@gmail.com",
      "password": "b7370338e54b1cb051be0dd54eba2f7f",
      "verificationCode": "210768",
      "createdAt": "2021-02-16T04:56:24.091Z",
      "updatedAt": "2021-02-16T04:56:24.091Z",
      "user_portfolios": [],
      "is_favourite": false
    }
  ]
1个回答

2

$lookup阶段后面添加以下阶段:

{
    $addFields: {
        is_favourite: { $arrayElemAt: ["$is_favourite.is_favourite", 0] }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接