0

I have to show the inspection count based upon the condition, if the user have edit inspection option then they can see the both published and unpublished file. If user don't have edit inspection then they have to see only published file.

[
  {
    inspection_count: {
      $sum: {
        $cond: {
          if: { $eq: [req.user.edit_inspection, "Y"] },
          then: {
            $cond: {
              if: { $eq: ["$projectdata.inspection_data.published", "N"] },
              then: 1,
              else: 0
            }
          },
          else: {
            $cond: {
              if: {
                $or: [
                  { $eq: ["$projectdata.inspection_data.published", "N"] },
                  { $eq: ["$projectdata.inspection_data.published", "Y"] }
                ]
              },
              then: 1,
              else: 0
            }
          }
        }
      }
    }
  }
];

output :

 $$hashKey: "object:38"
    architect_name: "name"
    cover_img: "1544161720747-SxS7923I4mWhLYUS.png"
    created_date: "2018-12-07T10:55:38.762Z"
    customer_id: ["0000000019130e361c3cf1eb"]
    download_access: "Y"
    file: false
    folder_name: "Garden Tech"
    inspection_count: 0 // this count should be change based upon condition
    location_name: "Chennai"
    parent_id: ["0000000019130e361c3cf1ec"]
    share: "N"
    share_id: (2) ["0000000056f3141be8c9df3e", "0000000077b4a71ad0b917e7"]
    user_id: "00000000400376127c1a8a31"
    _id: "000000002b22c8308cd29cde"

I tried the above code . Kindly look on this.

Vishnu
  • 745
  • 12
  • 32

0 Answers0