how to convert timestamp to date in mongodb?

前端 未结 2 1262
一向
一向 2020-12-18 00:43

i am working on aggregation of mongodb collection.my mongodb collection has creation_time in timestamp.How will i can collect same day result and aggregate with next day res

相关标签:
2条回答
  • 2020-12-18 01:05

    In my case , I used

    db.collection.aggregate([
      { "$project": {
        "date": { "$toDate": { 
            $multiply:["$_id",1000 ]
                 } 
           }
      }}
    ])
    

    convert timestamp from milliseconds to seconds.

    0 讨论(0)
  • 2020-12-18 01:15

    You can use $toDate aggregation to convert timestamp to ISO date and $toLong to convert string timestamp to integer value in mongodb 3.6

    db.collection.aggregate([
      { "$project": {
        "_id": {
          "$toDate": {
            "$toLong": "$_id"
          }
        }
      }},
      { "$group": {
        "_id": { "$dateToString": { "format": "%Y-%m-%d", "date": "$_id" } },
        "count": { "$sum": 1 }
      }}
    ])
    

    Try it here

    And with the previous versions

    db.collection.aggregate([
      { "$project": {
        "date": { "$add": [ new Date(0), "$_id" ] }
      }}
    ])
    
    0 讨论(0)
提交回复
热议问题