美文网首页
mongo日期格式转换

mongo日期格式转换

作者: ManrayHsu | 来源:发表于2021-09-07 09:11 被阅读0次

    示例

    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
             }
         }
     ]);    
    

    备注:match查询条件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日期格式转换

          本文链接:https://www.haomeiwen.com/subject/kzyuwltx.html