title: MapReduce in PyMongo
body: <p>My Mongo collection : <code>Impressions</code> has docs in the following format:-</p>
  
  <pre><code>   {
          _uid: 10,
          "impressions": [
              {
                  "pos": 6,
                  "id": 123,
                  "service": "furniture"
              },
              {
                  "pos": 0,
                  "id": 128,
                  "service": "electronics"
              },
              {
                  "pos": 2,
                  "id": 127,
                  "service": "furniture"
              },
              {
                  "pos": 2,
                  "id": 125,
                  "service": "electronics"
              },
              {
                  "pos": 10,
                  "id": 124,
                  "service": "electronics"
              }
          ]
        },
       {
          _uid: 11,
          "impressions": [
              {
                  "pos": 1,
                  "id": 124,
                  "service": "furniture"
              },
              {
                  "pos": 10,
                  "id": 124,
                  "service": "electronics"
              },
              {
                  "pos": 1,
                  "id": 123,
                  "service": "furniture"
              },
              {
                  "pos": 21,
                  "id": 122,
                  "service": "furniture"
              },
              {
                  "pos": 3,
                  "id": 125,
                  "service": "electronics"
              },
              {
                  "pos": 10,
                  "id": 121,
                  "service": "electronics"
              }
              ]
           },
              .
              .
              .
              .
              .
  </code></pre>
  
  <p>Each of the doc in the collection has <code>"impressions"</code> key which is an array of dictionaries. In each dictionary <code>"id"</code> is the id of the entity, <code>"service"</code> is the service type and <code>"pos"</code>is the position of the item in the search page results. My aim is to find out the count of number of impressions for every <code>"id"</code> in each category.
  So for the above data for <code>"service"</code> == <code>"furniture"</code>, I want to have this as my aggregation results:-</p>
  
  <pre><code>[
  {"id": 123,"impressions_count":2},
  {"id": 127,"impressions_count":1},
  {"id": 124,"impressions_count":1},
  {"id": 122,"impressions_count":1}
  ]
  </code></pre>
  
  <p>I tried to aggregate on the "id" using MAPREDUCE via following function in a python script</p>
  
  <pre><code>def fetch_impressions():
      try:
          imp_collection = get_mongo_connection('Impressions')
          map = Code("""
                  function(){
                      for( x in this.impressions){
                          var flat_id = x['id'];
                          var service_type = x['service']
                          emit(parseInt(flat_id),1);
                          }
                      };
                  """)
  
                          """)
          reduce = Code("""
                          function(a,b){
                              return Array.sum(b);
                              };
                          """)
  
          results = imp_collection.map_reduce(map, reduce, 'aggregation_result')
          return results
      except Exception as e:
          raise Exception(e)
  </code></pre>
  
  <p>But I'm getting the results as None, probably because of the faulty map function.I'm new to Javascript and Mongo kindly help!</p>