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

    XMLWordPrintable

Details

    • Bug
    • Status: Closed
    • Minor - P4
    • Resolution: Won't Fix
    • 2.6.4
    • None
    • MapReduce
    • None
    • ALL

    Description

      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 }

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: