MongoDB: 特定子文档所有字段的聚合值
MongoDB: Aggregate values of all fields of a specific subdocument
我正在尝试使用 MongoDB 的聚合框架从具有以下结构的集合中生成文档:
{
"_id" : ObjectId("5510eb56f877bbef153d236d"),
"attributes" : {
"brand" : "Mercedes",
"price" : 289,
"family" : "GARDEN"
},
"name" : "Bigger Fix Soft Crisps"
}
{
"_id" : ObjectId("5510eb56f877bbef153d236e"),
"attributes" : {
"brand" : "Adelholzener",
"price" : 683,
"family" : "COMPUTER"
},
"name" : "Soft Stockhome Chips"
}
{
"_id" : ObjectId("5510eb56f877bbef153d236f"),
"attributes" : {
"brand" : "Pepsi",
"price" : 789,
"family" : "CAR"
},
"name" : "Light Yearsailfresh Meat"
}
我想从 attributes
子文档中获取所有字段及其可能值的聚合。由于子文档中的字段未知,我不能简单地使用本例中给出的字段,因此必须是动态的。结果应与此类似:
{
"_id" : "brand",
"values" : [
"Mercedes", "Adelholzener", "Pepsi"
]
},
{
"_id" : "price",
"values" : [
289, 683, 789
]
},
{
"_id" : "family",
"values" : [
"GARDEN", "COMPUTER", "CAR"
]
}
到目前为止,我还没有找到解决这个特别问题的方法。我尝试使用 $project
和 $unwind
(这显然只适用于数组)。
试图弄清楚如何聚合,但我不确定是否可行,因为您无法以任何好的方式获取对象中的键。
这是一个 MapReduce 作业,可以执行此操作:
db.runCommand({
"mapreduce" : "test_collection", // Change collection here
"map" : function() {
for (var key in this.attributes) {
// Emit objects w array as we cannot reduce arrays directly
emit(key, {x: [this.attributes[key]]});
}
},
"reduce" : function(key, values) {
var res = [];
values.forEach(function (d) {
Array.prototype.push.apply(res, d.x);
});
return {x: res};
},
"finalize": function (key, reducedVal) {
return reducedVal.x;
},
"out": { inline: 1 }
});
我正在尝试使用 MongoDB 的聚合框架从具有以下结构的集合中生成文档:
{
"_id" : ObjectId("5510eb56f877bbef153d236d"),
"attributes" : {
"brand" : "Mercedes",
"price" : 289,
"family" : "GARDEN"
},
"name" : "Bigger Fix Soft Crisps"
}
{
"_id" : ObjectId("5510eb56f877bbef153d236e"),
"attributes" : {
"brand" : "Adelholzener",
"price" : 683,
"family" : "COMPUTER"
},
"name" : "Soft Stockhome Chips"
}
{
"_id" : ObjectId("5510eb56f877bbef153d236f"),
"attributes" : {
"brand" : "Pepsi",
"price" : 789,
"family" : "CAR"
},
"name" : "Light Yearsailfresh Meat"
}
我想从 attributes
子文档中获取所有字段及其可能值的聚合。由于子文档中的字段未知,我不能简单地使用本例中给出的字段,因此必须是动态的。结果应与此类似:
{
"_id" : "brand",
"values" : [
"Mercedes", "Adelholzener", "Pepsi"
]
},
{
"_id" : "price",
"values" : [
289, 683, 789
]
},
{
"_id" : "family",
"values" : [
"GARDEN", "COMPUTER", "CAR"
]
}
到目前为止,我还没有找到解决这个特别问题的方法。我尝试使用 $project
和 $unwind
(这显然只适用于数组)。
试图弄清楚如何聚合,但我不确定是否可行,因为您无法以任何好的方式获取对象中的键。
这是一个 MapReduce 作业,可以执行此操作:
db.runCommand({
"mapreduce" : "test_collection", // Change collection here
"map" : function() {
for (var key in this.attributes) {
// Emit objects w array as we cannot reduce arrays directly
emit(key, {x: [this.attributes[key]]});
}
},
"reduce" : function(key, values) {
var res = [];
values.forEach(function (d) {
Array.prototype.push.apply(res, d.x);
});
return {x: res};
},
"finalize": function (key, reducedVal) {
return reducedVal.x;
},
"out": { inline: 1 }
});