如何在mongodb中的$ group中给出多个条件

时间:2019-06-04 07:00:15

标签: node.js mongodb mongoose grouping

我必须根据条件显示检查计数,如果用户具有编辑检查选项,则他们可以看到已发布和未发布的文件。如果用户没有编辑检查,则他们只能看到发布的文件。

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

输出:

 $$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"

我尝试了上面的代码。请看一下。

0 个答案:

没有答案