可以汇总返回新日期吗?

时间:2016-12-14 03:17:09

标签: mongodb

该文件是:

{
  time: ISODate("2016-12-05T14:00:00.000+0000")
}

假设:

今天是“2016-12-06”,我想做一些聚合,它会返回

{
  time: ISODate("2016-12-06T14:00:00.000+0000")
}

今天是“2016-12-07”,它将返回

{
  time: ISODate("2016-12-07T14:00:00.000+0000")
}

所以,结果是现在的日期+字段的时间

我咨询了所有Aggregation Pipeline Operators并找到了一些Date Getters操作员,而不是Date Setters

1 个答案:

答案 0 :(得分:0)

以下是您问题的解决方案。

db.getCollection('myCollection').aggregate([
    {
        $project : {
            "customDate":  { $add : new Date() }
        }
    }
])

上述查询的输出将如下所示。

{
    "_id" : ObjectId("584d3fdf08e6815847d15fe7"),
    "customDate" : ISODate("2016-12-14T03:23:20.077Z")
}
相关问题