首页 > 解决方案 > unix时间戳列时如何按日期对文档进行分组?

问题描述

我在 mongodb 中有以下集合

[
      {
        "createdAt": 1596700221742,
        "id": "5f2bb63da9babddd420d0fa6",
        "quoteId": "SBA0005",
        "commission": 0,
      },
      {
        "createdAt": 1596699868976,
        "id": "5f2bb4dcbcf1acdbb64137d0",
        "quoteId": "SBA0004",
        "commission": 0,
      }]

我们如何按日期分组?

标签: mongodbmongoosemongodb-querysails-mongo

解决方案


试试这个:

db.collection.aggregate([
  {
    $group: {
      _id: {
        y: { $year: { $toDate: "$createdAt" }},
        m: { $month: { $toDate: "$createdAt" }},
        d: { $dayOfMonth: { $toDate: "$createdAt" }}
      }
    }
  }
])

它很可能不会给出您正在寻找的结果。但是,根据您的输入,这就是可以作为解决方案提供的全部内容。


推荐阅读