首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB聚集普查

MongoDB聚集普查
EN

Stack Overflow用户
提问于 2022-01-03 15:54:12
回答 1查看 46关注 0票数 0

我想把这些数据汇总起来,我需要按月来统计数据,比如人口普查。对于我有到达日期和出发日期的每一件物品,这个项目在这个范围内有一个月的总和+1。我该怎么做?

数据:

代码语言:javascript
复制
[{
    _id:1
    history:
    [
       {_id:1, date:2021-03-01, type:"Departure"},     
       {_id:2, date:2021-01-01, type:"Arrival"}
    ]
},
{
    _id:2
    history:
    [
       {_id:1, date:2021-07-03, type:"Departure"}, 
       {_id:2, date:2021-02-10, type:"Arrival"}
    ]
},
{
    _id:3
    history:
    [
       {_id:1, date:2021-08-22, type:"Departure"}, 
       {_id:2, date:2021-11-5, type:"Arrival"}
    ]
}]

结果:

代码语言:javascript
复制
    {month:1, year:2021, counter:1},
    {month:2, year:2021, counter:2},
    {month:3, year:2021, counter:2},
    {month:3, year:2021, counter:1},
    {month:4, year:2021, counter:1},
    {month:5, year:2021, counter:1},
    {month:6, year:2021, counter:1},
    {month:7, year:2021, counter:0},
    {month:8, year:2021, counter:1},
    {month:9, year:2021, counter:1},
    {month:10, year:2021, counter:1},
    {month:11, year:2021, counter:1},
    {month:12, year:2021, counter:0}
]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-04 01:53:16

我更改您的数据_id=3,因为出发日期应该大于到达日期。

  • $match
  • $set
  • $set
  • $set
  • $project
  • $unwind
  • $group
  • $project
  • $sort
代码语言:javascript
复制
db.collection.aggregate([
  {
    "$match": {}
  },
  {
    "$set": {
      arrival: {
        $dateFromString: {
          dateString: {
            $arrayElemAt: [ "$history.date", 1 ]
          }
        }
      },
      departure: {
        $dateFromString: {
          dateString: {
            $arrayElemAt: [ "$history.date", 0 ]
          }
        }
      }
    }
  },
  {
    "$set": {
      monthDiff: {
        $dateDiff: {
          startDate: "$arrival",
          endDate: "$departure",
          unit: "month"
        }
      }
    }
  },
  {
    "$set": {
      historyRange: {
        "$map": {
          "input": { $range: [ 0, { $add: [ "$monthDiff", 1 ] }, 1 ] },
          "as": "m",
          "in": {
            item: {
              $dateAdd: {
                startDate: "$arrival",
                unit: "month",
                amount: "$$m"
              }
            }
          }
        }
      }
    }
  },
  {
    "$project": {
      historyRange: {
        "$map": {
          "input": "$historyRange",
          "as": "h",
          "in": {
            year: { $year: "$$h.item" },
            month: { $month: "$$h.item" }
          }
        }
      }
    }
  },
  {
    "$unwind": "$historyRange"
  },
  {
    "$group": {
      "_id": "$historyRange",
      "counter": {
        "$sum": 1
      }
    }
  },
  {
    "$project": {
      _id: 0,
      counter: 1,
      month: "$_id.month",
      year: "$_id.year"
    }
  },
  {
    "$sort": { month: 1 }
  }
])

蒙古操场

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70568293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档