Get true or false ( only value not array or object ) from mongodb lookup aggregation

Viewed 248

I am trying to make a function that returns if a user is a favorite or not.

I am using MongoDB aggregate on User model with lookout with favourites model ( in which all favourite user has saved ) with pipeline and $project

here is the MongoDB aggregate function query

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',
                },
            },
        ]);

and I am getting this result

 [
    {
      "_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
        }
      ]
    }
  ]

but I want to get is_favourite: true instead of an array of object I need this output from 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 Answers

Just add below stage after $lookup stage:

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