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

Group command does not support code with scope for the key and finalize functions

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.6.4
    • Component/s: MapReduce
    • None
    • Query
    • ALL

      The group command DOES support code with scope for the reduce function, but does NOT support code with scope for the key and finalize functions.

      Here's a C# test to reproduce the issue with the key function:

      [Test]
      public void TestKeyFunctionWithScope()
      {
          var reduceFunction = new BsonJavaScript("function(document, result) { result.z = 2 }");
          var keyFunction = new BsonJavaScriptWithScope(
              "function(document) { return { x : document[key] }; }",
              new BsonDocument("key", "x"));
          var result = _collection.Group(new GroupArgs
          {
              KeyFunction  = keyFunction,
              ReduceFunction = reduceFunction,
              Initial = new BsonDocument("z", 0)
          })
          .ToList();
          Assert.That(result.Count, Is.EqualTo(1));
          Assert.That(result[0]["z"].ToInt32(), Is.EqualTo(2));
      }
      

      The result is:

      Command 'group' failed: exception: ReferenceError: key is not defined
      

      Here's a C# test to reproduce the issue with the finalize function:

      [Test]
      public void TestFinalizeFunctionWithScope()
      {
          var reduceFunction = new BsonJavaScript("function(document, result) { result.z = 1; }");
          var finalizeFunction = new BsonJavaScriptWithScope(
              "function(result) { result.z = z }",
              new BsonDocument("z", 2));
          var result = _collection.Group(new GroupArgs
          {
              KeyFields = GroupBy.Keys("x"),
              ReduceFunction = reduceFunction,
              FinalizeFunction = finalizeFunction,
              Initial = new BsonDocument("z", 0)
          })
          .ToList();
          Assert.That(result.Count, Is.EqualTo(1));
          Assert.That(result[0]["z"].ToInt32(), Is.EqualTo(2));
      }
      

      The result is:

      Command 'group' failed: exception: ReferenceError: z is not defined
      

      The ASSERTs in the above tests assume a test collection with the following documents:

      { _id : ..., x : 1 }
      { _id : ..., x : 1 }
      

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: