Map Reduce with mongo on nested document

坚强是说给别人听的谎言 提交于 2019-12-22 17:29:10

问题


I have the following document structure:

{
  "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)"
  }
}

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.


回答1:


Mongo map-reduce examples can be found here: http://docs.mongodb.org/manual/tutorial/map-reduce-examples/

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 }



回答2:


it seems that regions should be an array

 "regions" : [{
    "region_id" : 796,
    "region_name" : "Australien: New South Wales (Sydney)"
  }]

"i want to count the properties country_id, ... "

It seems you want this output.

...
{_id:  328, cities: 6, regions: 1},
{_id:  329, cities: 10, regions: 4},
...

try experimenting with the following, noting that it will only sum the cities array.

db.Country.aggregate(
  { $unwind : "$regions" },  {'$group': {'_id': '$country_id' , 'cities' : { $sum : 1}   } }
)

the following will provide a output similar to the accepted answer.

db.Country.aggregate(
{'$group': {'_id': '$country_id' , 'cities' : { $push: "$cities.city_id" }, 'regions' : {    $push: "$regions.region_id" }   }  }
)


来源:https://stackoverflow.com/questions/16319283/map-reduce-with-mongo-on-nested-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!