mongodb - 按内部数组中的对象分组(mongodb 聚合)
问题描述
我有像这样的文档:
{
"products": [
{
"uid": "aside-11kka-asdm12-asdjl"
"price": 123,
"count": 123
}
]
}
我想将它分组到产品数组中,使用聚合(产品 uid 可以在另一个对象中重复)
解决方案
您需要$unwind
首先对嵌套数组进行分组
db.collection.aggregate([
{
"$unwind": "$products"
},
{
$group: {
"_id": "$products.uid",
"count": {
$push: "$products.count"
},
"price": {
$push: "$products.price"
}
}
}
])
推荐阅读
- powerbi - Power Bi 帮助 - 在带有“条件”的卡片中显示值
- sql - 在dataTable asp .net核心中显示除Id之外的其他值
- laravel - Vue 没有收到来自 api 的所有数据
- java - 启动时启动画面动画循环
- vue.js - Vue 3 Composition API...如何替换 getElementById
- java - 如何在 Quarkus 中禁用实时重新加载?
- python - TWS API:使用 reqAccountUpdates 创建带有仓位的数据框
- python - 使用 Amazon Textract 分析 PDF 的特定页面
- reactjs - 对象是来自 userReducer 的“未知”类型
- c++ - C++Builder 中的 Android 推送通知