mongodb - mongo 聚合框架按季度/半年/年分组
问题描述
我有一个具有这种模式结构的数据库:
{
"name" : "Carl",
"city" : "paris",
"time" : "1-2018",
"notes" : [
"A",
"A",
"B",
"C",
"D"
]
}
这个查询使用聚合框架:
db.getCollection('collection').aggregate(
[{
"$match": {
"$and": [{
"$or": [ {
"time": "1-2018"
}, {
"time": "2-2018"
} ]
}, {
"name": "Carl"
}, {
"city": "paris"
}]
}
}, {
"$unwind": "$notes"
}, {
"$group": {
"_id": {
"notes": "$notes",
"time": "$time"
},
"count": {
"$sum": 1
}
}
}
, {
"$group": {
"_id": "$_id.time",
"count": {
"$sum": 1
}
}
}, {
"$project": {
"_id": 0,
"time": "$_id",
"count": 1
}
}])
它工作正常,我得到了这些结果:
{
"count" : 4.0,
"time" : "2-2018"
}
{
"count" : 4.0,
"time" : "1-2018"
}
我的问题是我想保持相同的比赛阶段,我想按季度分组。
这是我想要的结果:
{
"count" : 8.0,
"time" : "1-2018" // here quarter 1
}
谢谢
解决方案
推荐阅读
- sql-server - 有没有办法在 MS SQL Server 中创建“变量”
- laravel - 无法连接到已部署应用程序 Elastic Beanstalk 中的 RDS
- python - 烧瓶 404 错误
- postgresql - 无法从 redash 连接到 localhost
- java - Spring Boot 2.0 CrudRepository 保存方法默认不刷新
- facebook - Facebook 测试用户标签位置
- javascript - 无法将外部 JSON 文件中的数据读取到我的 HTML 页面
- powerbi - 如何将 Power BI 报表嵌入到组织应用程序中
- javascript - 在 axios 的 POST 请求中发送参数
- java - Android Recyclerview 添加其他视图