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

If map function string ends in a semicolon mapReduce fails silently returning no results

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.4.9
    • Component/s: JavaScript
    • Labels:
      None
    • ALL
    • Hide

      Create some test data:

      use test
      db.test.drop()
      db.test.insert({ _id : 1, x : 1 })
      db.test.insert({ _id : 2, x : 2 })
      db.test.insert({ _id : 3, x : 2 })
      

      Define map and reduce functions and call mapReduce:

      var map = "function map() { emit(this.x, 1); }"
      var reduce = "function reduce(key, values) { return values.length; }"
      
      db.test.mapReduce(map, reduce, { out : { inline : 1 } })
      

      The result is:

      {
              "results" : [
                      {
                              "_id" : 1,
                              "value" : 1
                      },
                      {
                              "_id" : 2,
                              "value" : 2
                      }
              ],
              "timeMillis" : 12,
              "counts" : {
                      "input" : 3,
                      "emit" : 3,
                      "reduce" : 1,
                      "output" : 2
              },
              "ok" : 1
      }
      

      Now add a semicolon to the end of the string for the map function:

      var map = "function map() { emit(this.x, 1); };"
      
      db.test.mapReduce(map, reduce, { out : { inline : 1 } })
      

      This time the result is:

      {
              "results" : [ ],
              "timeMillis" : 14,
              "counts" : {
                      "input" : 3,
                      "emit" : 0,
                      "reduce" : 0,
                      "output" : 0
              },
              "ok" : 1
      }
      

      Note the empty results array and the 0 emit count.

      Show
      Create some test data: use test db.test.drop() db.test.insert({ _id : 1, x : 1 }) db.test.insert({ _id : 2, x : 2 }) db.test.insert({ _id : 3, x : 2 }) Define map and reduce functions and call mapReduce: var map = "function map() { emit( this .x, 1); }" var reduce = "function reduce(key, values) { return values.length; }" db.test.mapReduce(map, reduce, { out : { inline : 1 } }) The result is: { "results" : [ { "_id" : 1, "value" : 1 }, { "_id" : 2, "value" : 2 } ], "timeMillis" : 12, "counts" : { "input" : 3, "emit" : 3, "reduce" : 1, "output" : 2 }, "ok" : 1 } Now add a semicolon to the end of the string for the map function: var map = "function map() { emit( this .x, 1); };" db.test.mapReduce(map, reduce, { out : { inline : 1 } }) This time the result is: { "results" : [ ], "timeMillis" : 14, "counts" : { "input" : 3, "emit" : 0, "reduce" : 0, "output" : 0 }, "ok" : 1 } Note the empty results array and the 0 emit count.

      If a string representing the map function for mapReduce ends in a semicolon the mapReduce fails silently and no results are returned.

      I'm not saying this should necessarily work. But it should either work or result in an error. It's the silent part that's the problem.

            Assignee:
            ramon.fernandez@mongodb.com Ramon Fernandez Marina
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: