mongodb聚合截断日期函数?

时间:2018-01-20 01:00:54

标签: mongodb

之前曾与许多其他SQL数据库合作过;比如Postgres和BigQuery

  1. https://www.postgresql.org/docs/10/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
  2. https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#timestamp_trunc
  3. 想知道mongodb是否有DATE_TRUNC函数,我找到了$ trunc但它仅适用于数字,我想要一个DATE_TRUNC函数将给定的Date(其他SQL数据库中的时间戳类型)截断到特定的边界,比如开始当年,月初,小时开始,可以通过获取年,月,日,小时来撰写新日期;有没有人与现有的agg运营商有一些解决方法?避免在应用程序代码中处理? 特别是对于WEEK的开始时刻和ISOWEEK的开始,有没有人有一个好的解决方法? https://docs.mongodb.com/manual/reference/operator/aggregation/trunc/

3 个答案:

答案 0 :(得分:1)

通过对日期或时间戳字段进行算术可以获得ISO周的开始,这里的周开始是Monday(1),周末是Sunday(7)

db.dd.aggregate(
    [
        {
            $addFields : { 
                startOfWeek : 1, // Monday
                currentDayOfWeek : {$dayOfWeek : "$date"}, 
                daysToMinus : { $subtract : [{$dayOfWeek : "$date"} , 1] },
                startOfThisWeek : { $subtract : [ "$date", {$multiply : [{ $subtract : [{$dayOfWeek : "$date"} , 1 ] }, 24, 60, 60, 1000 ]  } ] }
            }
        }
    ]
).pretty()

文件

> db.dd.find()
{ "_id" : ObjectId("5a62e2697702c6be61d672f4"), "date" : ISODate("2018-01-20T06:32:09.157Z") }

周开始

{
    "_id" : ObjectId("5a62e2697702c6be61d672f4"),
    "date" : ISODate("2018-01-20T06:32:09.157Z"),
    "startOfWeek" : 1,
    "currentDayOfWeek" : 7,
    "daysToMinus" : 6,
    "startOfThisWeek" : ISODate("2018-01-14T06:32:09.157Z")
}
> 

答案 1 :(得分:0)

可以使用$dateFromParts函数将日期截断为等周:

例如

db.dd.aggregate(
  {
    $dateFromParts: {
      isoWeekYear: { $isoWeekYear: "$date" },
      isoWeek: { $isoWeek: "$date" }
    }
  }
)

对于Fri, 22 Jun 2018 20:46:50 UTC +00:00,它返回Fri, 18 Jun 2018 00:00:00 UTC +00:00

要截断到小时,天,月等,使用$dateFromString$dateToString会更容易。以下示例将日期截断为小时:

db.dd.aggregate(
  {
    $dateFromString: {
      dateString: { 
        $dateToString: {
          format: '%Y-%m-%dT%H:00:00+00:00',
          date: '$date'
        }
      }
    }
  }
)

答案 2 :(得分:0)

可以组合$dateToParts$dateFromParts

小时分钟:< / p>

db.getCollection("data").aggregate([
  {"$addFields": {
     "dateVarFull": {"$dateToParts": {date: {"$toDate" : "2020-08-27T13:00:00Z"}}}
  }},
  {"$addFields": {
    "dateVarTrunc": { "$dateFromParts": {
      'year': "$dateVarFull.year",
      'month': "$dateVarFull.month",
      'day': "$dateVarFull.day"
    }}
  }}
])

结果:

{
    "dateVarFull" : {
        "year" : NumberInt(2020), 
        "month" : NumberInt(8), 
        "day" : NumberInt(27), 
        "hour" : NumberInt(13), 
        "minute" : NumberInt(0), 
        "second" : NumberInt(0), 
        "millisecond" : NumberInt(0)
    }, 
    "dateVarTrunc" : ISODate("2020-08-27T00:00:00.000+0000")
}

对于截断,请使用iso8601: true参数:

db.getCollection("data").aggregate([
  {"$addFields": {
    "dateVarFull": {
       "$dateToParts": {
         date: {"$toDate" : "2020-08-27T13:00:00Z"},
         iso8601: true
       }
    }
  }},
  {"$addFields": {
    "dateVarTrunc": { "$dateFromParts": {
      'isoWeekYear': "$dateVarFull.isoWeekYear",
      'isoWeek': "$dateVarFull.isoWeek",
      'isoDayOfWeek': 1
    }}
  }}
])

结果:

{
    "dateVarFull" : {
        "isoWeekYear" : NumberInt(2020), 
        "isoWeek" : NumberInt(35), 
        "isoDayOfWeek" : NumberInt(4), 
        "hour" : NumberInt(13), 
        "minute" : NumberInt(0), 
        "second" : NumberInt(0), 
        "millisecond" : NumberInt(0)
    }, 
    "dateVarTrunc" : ISODate("2020-08-24T00:00:00.000+0000")
}