Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-11427

Docs for SERVER-27100: ExpressionObject does not optimize itself to a constant expression

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Fixed
    • Icon: Major - P3 Major - P3
    • 3.7.3
    • None
    • manual

    Description

      Documentation Request Summary:

      There's a note on the $bucket docs page (in the 'boundaries' argument) about how documents must be wrapped in $literal, which is no longer true (so long as all the values are constants)

      https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/

      Engineering Ticket Description:

      An ExpressionObject's implementation of optimize() is missing an optimization where if all of the values in the object are constant, the entire ExpressionObject can be replaced with an ExpressionConstant representing the object literal.

      This can cause an confusing error message when using the $bucket stage, since $bucket ensures all entries of the boundaries array are constants:

      > db.baz.find()
      { "_id" : ObjectId("582e293f38b7ba11ec92eda8"), "a" : { "b" : 1, "c" : 1 } }
      { "_id" : ObjectId("582e293f38b7ba11ec92eda9"), "a" : { "b" : 1, "c" : 2 } }
       
      // This fails, implying the object literals are not constants:
      > db.baz.aggregate([{$bucket: {groupBy: "$a", boundaries: [{b: 1, c: 1}, {b: 1, c: 2}, {b: 2, c: 3}]}}])
      assert: command failed: {
      	"ok" : 0,
      	"errmsg" : "The $bucket 'boundaries' field must be an array of constant values, but found value: { b: 1.0, c: 1.0 }.",
      	"code" : 40191,
      	"codeName" : "Location40191"
      } : aggregate failed
      _getErrorWithCode@src/mongo/shell/utils.js:25:13
      doassert@src/mongo/shell/assert.js:16:14
      assert.commandWorked@src/mongo/shell/assert.js:370:5
      DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
      @(shell):1:1
       
      2016-11-17T17:05:06.583-0500 E QUERY    [main] Error: command failed: {
      	"ok" : 0,
      	"errmsg" : "The $bucket 'boundaries' field must be an array of constant values, but found value: { b: 1.0, c: 1.0 }.",
      	"code" : 40191,
      	"codeName" : "Location40191"
      } : aggregate failed :
      _getErrorWithCode@src/mongo/shell/utils.js:25:13
      doassert@src/mongo/shell/assert.js:16:14
      assert.commandWorked@src/mongo/shell/assert.js:370:5
      DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
      @(shell):1:1
       
      // If the objects are wrapped in a $literal, the $bucket succeeds:
      > db.baz.aggregate([{$bucket: {groupBy: "$a", boundaries: [{$literal: {b: 1, c: 1}}, {$literal: {b: 1, c: 2}}, {$literal: {b: 2, c: 3}}]}}])
      { "_id" : { "b" : 1, "c" : 1 }, "count" : 1 }
      { "_id" : { "b" : 1, "c" : 2 }, "count" : 1 }
      

      Attachments

        Activity

          People

            andrew.aldridge@mongodb.com Andrew Aldridge
            kay.kim@mongodb.com Kay Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              5 years, 37 weeks, 1 day ago