我正在使用MongoDB聚合框架尝试从我们的数据集中收集一些总计。
以下是源数据的示例:
{
"urn": "urn:content:epi:0001",
"duration": 3450272,
"profiles": {
"low": {
"mp3": {
"size": 8623059425,
"url": "0001_low.mp3"
},
"oga": {
"size": 8623059425,
"url": "0001_low.oga"
},
"m4a": {
"size": 8623059425,
"url": "0001_low.m4a"
}
},
"medium": {
"mp3": {
"size": 8623059425,
"url": "0001_medium.mp3"
},
"oga": {
"size": 8623059425,
"url": "0001_medium.oga"
},
"m4a": {
"size": 8623059425,
"url": "0001_medium.m4a"
}
},
"high": {
"mp3": {
"size": 8623059425,
"url": "0001_high.mp3"
},
"oga": {
"size": 8623059425,
"url": "0001_high.oga"
},
"m4a": {
"size": 8623059425,
"url": "0001_high.m4a"
}
}
}
}
我要做的是将每个profile.(low|medium|high).(mp3|oga|m4a)
分成单独的文档/项目进行聚合,例如:
{
"_id": null,
"files": [
{
"urn": "urn:content:epi:0001",
"duration": 3450272,
"size": 8623059425,
"url": "0001_low.mp3"
},
{
"urn": "urn:content:epi:0001",
"duration": 3450272,
"size": 8623059425,
"url": "0001_low.oga"
},
{
"urn": "urn:content:epi:0001",
"duration": 3450272,
"size": 8623059425,
"url": "0001_low.m4a"
},
{
"urn": "urn:content:epi:0001",
"duration": 3450272,
"size": 8623059425,
"url": "0001_medium.mp3"
},
{
"urn": "urn:content:epi:0001",
"duration": 3450272,
"size": 8623059425,
"url": "0001_medium.oga"
},
{
"urn": "urn:content:epi:0001",
"duration": 3450272,
"size": 8623059425,
"url": "0001_medium.m4a"
},
{
"urn": "urn:content:epi:0001",
"duration": 3450272,
"size": 8623059425,
"url": "0001_high.mp3"
},
{
"urn": "urn:content:epi:0001",
"duration": 3450272,
"size": 8623059425,
"url": "0001_high.oga"
},
{
"urn": "urn:content:epi:0001",
"duration": 3450272,
"size": 8623059425,
"url": "0001_high.m4a"
}
]
}
这种事情是否可以使用聚合框架,或者MapReduce可以做什么?
答案 0 :(得分:0)
不,聚合框架的$unwind
阶段是将单个集合文档转换为多个管道文档的唯一方法,$unwind
仅适用于数组,而不适用于像您这样的子文档。