如果 $match 条件总计不满足,如何将此 mongo 查询更改为 return "00:00"?

How to change this mongo query to return "00:00" if $match condition does not satisfy in aggregate?

假设我有一个这样的时间表集合:

[
  {
     _id: 1,
     createdBy: "John",
     duration: "00:30"
  },
  {
     _id: 2,
     createdBy: "John",
     duration: "01:30"
  },
  {
     _id: 3,
     createdBy: "Mark",
     duration: "00:30"
  },
]

我的输入是一组用户名:

["John", "Mark", "Rikio"]

我想使用 mongodb 聚合来计算用户名数组中每个用户的时间表总持续时间,如果没有找到时间表,它应该 return duration: "00:00 ”。例如,它应该 return:

[
   {createdBy: "John", totalDuration: "02:00"}, 
   {createdBy: "Mark", totalDuration: "00:30"}, 
   {createdBy: "Rikio", totalDuration: "00:00"}
]

但是,当我使用 $match 查询时,如果没有时间表,它不会 return 任何东西,所以我不知道哪个用户没有时间表和 return "00: 00" 给他们。

我完全同意@turivishal,但仍然可以通过 mongo 一个丑陋的查询来完成。

db.collection.aggregate([
  {
    $match: {}
  },
  {
    $set: {
      minutes: {
        $let: {
          vars: {
            time: {
              $split: [
                "$duration",
                ":"
              ]
            }
          },
          in: {
            "$add": [
              {
                "$multiply": [
                  {
                    $toInt: {
                      $first: "$$time"
                    }
                  },
                  60
                ]
              },
              {
                $toInt: {
                  $last: "$$time"
                }
              }
            ]
          }
        }
      }
    }
  },
  {
    $group: {
      "_id": "$createdBy",
      "totalMinutes": {
        "$sum": "$minutes"
      }
    }
  },
  {
    $group: {
      "_id": null,
      "docs": {
        "$push": "$$ROOT"
      }
    }
  },
  {
    $set: {
      "docs": {
        $map: {
          "input": [
            "John",
            "Mark",
            "Rikio"
          ],
          "as": "name",
          "in": {
            $let: {
              vars: {
                findName: {
                  "$first": {
                    "$filter": {
                      "input": "$docs",
                      "as": "d",
                      "cond": {
                        "$eq": [
                          "$$d._id",
                          "$$name"
                        ]
                      }
                    }
                  }
                }
              },
              in: {
                "$cond": {
                  "if": "$$findName",
                  "then": "$$findName",
                  "else": {
                    _id: "$$name",
                    totalMinutes: 0
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    $unwind: "$docs"
  },
  {
    $replaceRoot: {
      "newRoot": "$docs"
    }
  },
  {
    $set: {
      "hours": {
        $floor: {
          "$divide": [
            "$totalMinutes",
            60
          ]
        }
      },
      "minutes": {
        "$mod": [
          "$totalMinutes",
          60
        ]
      }
    }
  },
  {
    $set: {
      "hours": {
        "$cond": {
          "if": {
            "$lt": [
              "$hours",
              10
            ]
          },
          "then": {
            "$concat": [
              "0",
              {
                "$toString": "$hours"
              }
            ]
          },
          "else": {
            "$toString": "$hours"
          }
        }
      },
      "minutes": {
        "$cond": {
          "if": {
            "$lt": [
              "$minutes",
              10
            ]
          },
          "then": {
            "$concat": [
              "0",
              {
                "$toString": "$minutes"
              }
            ]
          },
          "else": {
            "$toString": "$minutes"
          }
        }
      }
    }
  },
  {
    $project: {
      duration: {
        "$concat": [
          "$hours",
          ":",
          "$minutes"
        ]
      }
    }
  }
])

mongoplayground