MongoDB展平嵌入式阵列

时间:2014-09-10 12:23:48

标签: mongodb mapreduce reporting

我想创建一个集合的报告。它的架构是: (我简化了架构,专注于问题)

Mongoose Schema

var MobilHomeSchema = new Schema({
    id: Schema.Types.ObjectId,
    region: String,
    equipments:[
        { id: ObjectId, label: String }
    ]
});

它包含许多mobilhomes。这些mobilhomes在一个露营地,在一个地区(我选择了这个群体,它可能是国家,......)。每个mobilhome都有一些设备,并不总是相同。

我想创建一个包含这些列的电子表格,以计算一个地区中每台设备的数量(这只是一个例子)

预期的通用结果格式

region | equipments.label 1 | equipments.label 2 | equipments.label 3 | ....

“真实”值的示例:

region|terrace|pergola|shower
Spain |   30  |  15   |150
France|   55  |  32   |540
...

以json格式,可能是:

修改

[{
  region: "Spain",
  terrace: 30,
  pergola: 15,
  shower: 150
},
{
  region: "France",
  terrace: 55,
  pergola: 32,
  shower: 540
}]

/ EDIT

我该怎么办?

(map-reduce?最商业智能工具?)

非常感谢!

1 个答案:

答案 0 :(得分:1)

不要使用map / reduce。使用aggregation。在mongo shell中,

> db.mobile.aggregate([
    { "$unwind" : "$equipments" },
    { "$group" : { "_id" : { "region" : "$region", "label" : "$equipments.label" }, "count" : { "$sum" : 1 } } }
])

关于文件

{ "region" : "France", "equipments" : [ { "_id" : 0, "label" : "terrace" }, { "_id" : 1, "label" : "pergola" } ] },
{ "region" : "France", "equipments" : [ { "_id" : 0, "label" : "shower" }, { "_id" : 1, "label" : "pergola" } ] },
{ "region" : "Spain", "equipments" : [ { "_id" : 0, "label" : "terrace" }, { "_id" : 1, "label" : "shower" } ] },
{ "region" : "Spain", "equipments" : [ { "_id" : 0, "label" : "veranda" }, { "_id" : 1, "label" : "pergola" } ] }

结果是

{ "_id" : { "region" : "Spain", "label" : "veranda" }, "count" : 1 }
{ "_id" : { "region" : "Spain", "label" : "terrace" }, "count" : 1 }
{ "_id" : { "region" : "Spain", "label" : "shower" }, "count" : 1 }
{ "_id" : { "region" : "France", "label" : "shower" }, "count" : 1 }
{ "_id" : { "region" : "France", "label" : "pergola" }, "count" : 2 }
{ "_id" : { "region" : "Spain", "label" : "pergola" }, "count" : 1 }
{ "_id" : { "region" : "France", "label" : "terrace" }, "count" : 1 }

由于您正在使用阵列,大概您可能不会提前知道所有可能类型的设备,这使得将上述结果推回到聚合中每个区域的一个对象是一件笨重的事情。尝试。最好在客户端处理这些结果。