Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-51779

Strange MapReduce issue - not working without JSON.stringify

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Trivial - P5 Trivial - P5
    • None
    • Affects Version/s: None
    • Component/s: MapReduce, Shell
    • None
    • Environment:
      Ubuntu 16.04 and MacOS 10.15.6.
      With Mongo 4.2.1 and 4.4.0
    • Query Optimization

      Hello,

      Sorry if it is a duplicate, I didn't find any similar issue.

      We have a problem with a MapReduce we are writing. It works when we test the functions in the shell, all guidelines from documentation concerning reduce are fulfilled but we have empty result when executing it on a collection.

      I wrote a file to reproduce the problem :

      db = db.getSiblingDB('test');
      db.baskets.drop();
      
      for (const x of Array(5).keys()) {
          db.baskets.insert({"fruit": "orange"});
      }
      
      for (const x of Array(8).keys()) {
          db.baskets.insert({"fruit": "apple"});
      }
      
      for (const x of Array(27).keys()) {
          db.baskets.insert({"fruit": "banana"});
      }
      
      print("This should work")
      cursor = db.baskets.mapReduce(function () {
          emit("fruit", {[this.fruit]: 1});
      }, function (key, values) {
          return values.reduce(function (a, c) {
              for (const [fruit, count] of Object.entries(c)) {
                  if (!a[fruit]) {
                      a[fruit] = 0;
                  }
                  a[fruit] += count;
              }
              return a;
          });
      }, {out: {inline: 1}});
      printjson(cursor);
      
      print("\n\nThis is the same but with a useless JSON.stringify of values in reduce")
      cursor = db.baskets.mapReduce(function () {
          emit("fruit", {[this.fruit]: 1});
      }, function (key, values) {
          JSON.stringify(values); // This line is completely useless... But it doesn't work without
          return values.reduce(function (a, c) {
              for (const [fruit, count] of Object.entries(c)) {
                  if (!a[fruit]) {
                      a[fruit] = 0;
                  }
                  a[fruit] += count;
              }
              return a;
          });
      }, {out: {inline: 1}});
      printjson(cursor);
      
      

      Here is the result :

      This should work
      {
      	"results" : [
      		{
      			"_id" : "fruit",
      			"value" : {
      				"banana" : 1
      			}
      		}
      	],
      	"ok" : 1
      }
      
      
      This is the same but with a useless JSON.stringify of values in reduce
      {
      	"results" : [
      		{
      			"_id" : "fruit",
      			"value" : {
      				"banana" : 27,
      				"apple" : 8,
      				"orange" : 5
      			}
      		}
      	],
      	"ok" : 1
      }
      

      I know I could write it using an aggregation but I want to emit more keys from other fields in the future instead of running multiple aggregations. (May be you have a better idea on this ?)

       

      Thank you !

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            benjamin.delacour@jobijoba.com Benjamin Delacour
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: