在嵌套文档上使用mongo进行Map Reduce [英] Map Reduce with mongo on nested document
本文介绍了在嵌套文档上使用mongo进行Map Reduce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下文档结构:
{
"country_id" : 328,
"country_name" : "Australien",
"cities" : [{
"city_id" : 19398,
"city_name" : "Bondi Beach (Sydney)"
}, {
"city_id" : 31102,
"city_name" : "Double Bay (Sydney)"
}, {
"city_id" : 31101,
"city_name" : "Rushcutters Bay (Sydney)"
}, {
"city_id" : 817,
"city_name" : "Sydney"
}, {
"city_id" : 31022,
"city_name" : "Wolly Creek (Sydney)"
}, {
"city_id" : 18851,
"city_name" : "Woollahra"
}],
"regions" : {
"region_id" : 796,
"region_name" : "Australien: New South Wales (Sydney)"
}
}
对于多面导航,我想计算属性country_id,citys.city_id,regions_region_id,我认为我可以使用地图/reduce做到这一点.
for a facetted navigation i want to count the properties country_id, cities.city_id, regions_region_id i think i can do this with map /reduce.
使用给定的结构可能吗?
Is this possible with the given structure ?
也许有人可以指向我正确的地图/缩小方向.
Maybe somebody can point me in the right map/reduce direction.
推荐答案
Mongo map-reduce examples can be found here: http://docs.mongodb.org/manual/tutorial/map-reduce-examples/
每个唯一的country_id,city_id和region_id元组的文档数都很简单:
The number of documents for each unique country_id, city_id, and region_id tuple is straightforward:
> function m() {
for(var i in this.cities) {
emit({country_id:this.country_id,
city_id:this.cities[i].city_id,
region_id:this.regions.region_id},
1);
} }
> function r(id,docs) {
return Array.sum(docs);
}
> db.loc.mapReduce(m,r,{out:"map_reduce_out"})
{
"result" : "map_reduce_out",
"timeMillis" : 5,
"counts" : {
"input" : 1,
"emit" : 6,
"reduce" : 0,
"output" : 6
},
"ok" : 1,
}
> db.map_reduce_out.find()
{ "_id" : { "country_id" : 328, "city_id" : 817, "region_id" : 796 }, "value" : 1 }
{ "_id" : { "country_id" : 328, "city_id" : 18851, "region_id" : 796 }, "value" : 1 }
{ "_id" : { "country_id" : 328, "city_id" : 19398, "region_id" : 796 }, "value" : 1 }
{ "_id" : { "country_id" : 328, "city_id" : 31022, "region_id" : 796 }, "value" : 1 }
{ "_id" : { "country_id" : 328, "city_id" : 31101, "region_id" : 796 }, "value" : 1 }
{ "_id" : { "country_id" : 328, "city_id" : 31102, "region_id" : 796 }, "value" : 1 }
这篇关于在嵌套文档上使用mongo进行Map Reduce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文