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

Objects are no longer treated as literals inside of an array of an aggregation expression

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical - P2
    • Resolution: Won't Fix
    • Affects Version/s: 3.2.1
    • Fix Version/s: None
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Operating System:
      ALL
    • Steps To Reproduce:
      Hide

      STEP ONE: Set up a collection (e.g. named j_bug) with the following two documents:

      { 
          "_id" : ObjectId("569fbe30f6916b01e49b27b7"), 
          "rand" : NumberInt(456), 
          "entities" : [
              {
                  "name" : "Test Entity"
              }
          ]
      }
      { 
          "_id" : ObjectId("569fbe8cf6916b01e49b27b9"), 
          "rand" : NumberInt(123)
      }
      

      STEP TWO: in mongo version 3.2.1 run the following aggregation

      db.j_bug.aggregate([
      {$project:{entities:{$ifNull:["$entities", [  {'name' : 'No Entity'} ] ]}}}
      ])
      >>Error from description will be produced.
      

      In mongo version 3.0.7 run the exact same aggregation query and you should get the desired output as follows:

      { "_id" : ObjectId("569fbe30f6916b01e49b27b7"), "entities" : [ { "name" : "Test Entity" } ] }
      { "_id" : ObjectId("569fbe8cf6916b01e49b27b9"), "entities" : [ { "name" : "No Entity" } ] }
      

      Show
      STEP ONE: Set up a collection (e.g. named j_bug) with the following two documents: { "_id" : ObjectId("569fbe30f6916b01e49b27b7"), "rand" : NumberInt(456), "entities" : [ { "name" : "Test Entity" } ] } { "_id" : ObjectId("569fbe8cf6916b01e49b27b9"), "rand" : NumberInt(123) } STEP TWO: in mongo version 3.2.1 run the following aggregation db.j_bug.aggregate([ {$project:{entities:{$ifNull:["$entities", [ {'name' : 'No Entity'} ] ]}}} ]) >>Error from description will be produced. In mongo version 3.0.7 run the exact same aggregation query and you should get the desired output as follows: { "_id" : ObjectId("569fbe30f6916b01e49b27b7"), "entities" : [ { "name" : "Test Entity" } ] } { "_id" : ObjectId("569fbe8cf6916b01e49b27b9"), "entities" : [ { "name" : "No Entity" } ] }

      Description

      On 3.0, an expression like the following will work:

      { $project: { _id: 0, entities:{ $ifNull : [ '$entities', [  {'name' : 'No Entity'} ] ] } }
      

      but on 3.2.1, it will generate the following error, since it tries to interpret the object literal {name: 'No Entity'} as an expression:

      assert: command failed: {
      	"ok" : 0,
      	"errmsg" : "field inclusion is not allowed inside of $expressions",
      	"code" : 16420
      } : aggregate failed
      _getErrorWithCode@src/mongo/shell/utils.js:23:13
      doassert@src/mongo/shell/assert.js:13:14
      assert.commandWorked@src/mongo/shell/assert.js:266:5
      DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5
      @(shell):1:1
       
      2016-01-20T13:02:55.869-0400 E QUERY    [thread1] Error: command failed: {
      	"ok" : 0,
      	"errmsg" : "field inclusion is not allowed inside of $expressions",
      	"code" : 16420
      } : aggregate failed :
      _getErrorWithCode@src/mongo/shell/utils.js:23:13
      doassert@src/mongo/shell/assert.js:13:14
      assert.commandWorked@src/mongo/shell/assert.js:266:5
      DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5
      @(shell):1:1
      

      The workaround is to wrap the object in a $literal expression, like so:

      { $project: { _id: 0, entities:{ $ifNull : [ '$entities', [  {$literal: {'name' : 'No Entity'} } ] ] } }
      

        Attachments

          Activity

            People

            Assignee:
            benjamin.murphy Benjamin Murphy
            Reporter:
            jesse_english Jesse English
            Participants:
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: