mongodb - mongodb聚合总和项作为嵌套数据
问题描述
这是我在收藏销售中的一些样本数据
[
{group:2, item:a, qty:3 },
{group:2, item:b, qty:3 },
{group:2, item:b, qty:2 },
{group:1, item:a, qty:3 },
{group:1, item:a, qty:5 },
{group:1, item:b, qty:5 }
]
我想查询如下数据并将热门组排序到顶部
[
{ group:1, items:[{name:'a',total_qty:8},{name:'b',total_qty:5} ],total_qty:13 },
{ group:2, items:[{name:'a',total_qty:3},{name:'b',total_qty:5} ],total_qty:8 },
]
实际上我们可以在服务器脚本(php,nodejs ...)中循环,但问题是分页。我不能使用跳过来获得正确的结果。
解决方案
推荐阅读
- python - 如何根据大小选择最近的对象
- java - 如何在 Android Studio Kotlin/Java 中设置颜色图标、文本标题栏?
- flutter - Flutter DART LINT 警告“尝试添加像‘动态’这样的显式类型,或在分析选项文件中启用隐式动态。”
- python - 将值与列表中的项目进行比较
- python - 使用 Python 从 Outlook .csv 附件中提取数据
- java - 过滤地图投影内的地点列表?
- android - 我可以在 Google Firestore 控制台中创建对象到对象映射吗?
- android - 以编程方式运动布局
- c - 使用 scanf 获取 C 中的命令和参数
- javascript - 您能否在 Chrome 中获得表示弹出窗口的 DOM 元素,而无需 var newWindow = window.open(但仅在开发工具打开时有效)?