mongo日期格式转换

示例

db.getCollection("doc").aggregate([
        {$match:{"createTime":{$gte:NumberLong(1630484863000),$lt:NumberLong(1630916863000)},
            "appUserId":{"$in":["f01d86f9556148e68f23eb5252967b67"]}
    }},
    {$project:
         {           
           groupTime:Date("$createTime"),
           groupTime1:new Date(Date("$createTime")),
           groupTime2: { $dateToString : {format: "%Y-%m-%d", date: new Date(Date("$createTime"))} },
           createTime:1
         }
     }
 ]);    

备注:project 列数据处理
createTime:1 显示createTime
执行结果

{ 
    "_id" : ObjectId("61319a60b3d666089a885b84"), 
    "createTime" : NumberLong(1630640736559), 
    "groupTime" : "Tue Sep 07 2021 09:05:45 GMT+0800", 
    "groupTime1" : ISODate("2021-09-07T01:05:45.000+0000"), 
    "groupTime2" : "2021-09-07"
}

NumberLong格式化为年月日

groupTime2: { $dateToString : {format: "%Y-%m-%d", date: new Date(Date("$createTime"))}},
结果: "groupTime2" : "2021-09-07"

NumberLong格式化为ISODate

groupTime2:new Date(Date("$createTime"))
结果: "groupTime1" : ISODate("2021-09-07T01:05:45.000+0000")

NumberLong格式化为date

groupTime:Date("$createTime")
结果:"groupTime" : "Tue Sep 07 2021 09:05:45 GMT+0800"

你可能感兴趣的:(mongo日期格式转换)