sql >> Database >  >> NoSQL >> MongoDB

Kaart verkleinen met mongo op genest document

Mongo map-reduce voorbeelden zijn hier te vinden:http://docs. mongodb.org/manual/tutorial/map-reduce-examples/

Het aantal documenten voor elke unieke country_id, city_id en region_id tuple is eenvoudig:

> 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 }


  1. MongoDB/PHP:verwijder element uit array

  2. ruby redis client scan vs sleutels

  3. Meteor.js-verzameling leeg op client

  4. Verschil tussen Numberlong(x) en Numberlong(x)