在2个MongoDB ISO日期之间获取日期

时间:2019-06-06 10:23:37

标签: mongodb aggregation-framework

我正在尝试获取2个ISODate之间的天差并将其放在days字段中。我在下面有这些文件:

{
    created_at: ISODate("2019-06-06TT00:00:00Z"),
    completed_at: "2019-06-08"
},
{
    created_at: ISODate("2019-06-06TT00:00:00Z"),
    completed_at: null
},
{
    created_at: ISODate("2019-06-06TT00:00:00Z"),
    completed_at: "2019-06-04"
}

到目前为止,我一直在尝试获取毫秒数。但是我不想要那个。我只想减去日期并得到天数的差异。

db.collection.aggregate([
            {
                $addFields: {
                    completed_at: {
                        $ifNull: [
                            {
                                "$dateFromString": {
                                    "dateString": "$completed_at"
                                }
                            },
                            new ISODate()
                        ]
                    }
                }
            },
            {
                $addFields: {
                    days: {
                        $cond: [
                            {"$lte": ["$completed_at", "$created_at"]},
                            0,
                            {
                                $divide: [
                                  {
                                    $subtract: [
                                        "$completed_at",
                                        "$created_at"
                                    ]
                                  },
                                    86400000
                                ]
                            }
                        ]
                    }
                }
            },
            {
                $project: {
                    _id: 0,
                    created_at: 1,
                    completed_at: 1,
                    days: 1
                }
            }
        ])

如果completed_at字段为null,则其值应为当前日期。如果completed_at小于created_at,则days为0。假设当前日期为2019年6月6日,则预期结果应为:

{
    days: 2
},
{
    days: 0
},
{
    days: 0
}

1 个答案:

答案 0 :(得分:0)

我认为我能够做到。我删除了日期字段中的时间,并且想要的结果正确显示。请告诉我是否正确安装,或者需要进行一些调整以使其更快。谢谢!

db.collection.aggregate([
  {
    $addFields: {
      created_at: {
        $dateFromParts: {
          "year": {
            $year: "$created_at"
          },
          "month": {
            $month: "$created_at"
          },
          "day": {
            $dayOfMonth: "$created_at"
          }
        }
      },
      completed_at: {
        $ifNull: [
          {
            "$dateFromString": {
              "dateString": "$completed_at"
            }
          },
          {
            $dateFromParts: {
              "year": {
                $year: new Date()
              },
              "month": {
                $month: new Date()
              },
              "day": {
                $dayOfMonth: new Date()
              }
            }
          }
        ]
      }
    }
  },
  {
    $addFields: {
      days: {
        $cond: [
          {
            "$lte": [
              "$completed_at",
              "$created_at"
            ]
          },
          0,
          {
            $divide: [
              {
                $subtract: [
                  "$completed_at",
                  "$created_at"
                ]
              },
              86400000
            ]
          }
        ]
      }
    }
  },
  {
    $project: {
      _id: 0,
      created_at: 1,
      completed_at: 1,
      days: 1
    }
  }
])