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

Date operators incorrectly throw exceptions for object arguments

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.5.3
    • Affects Version/s: 2.4.1
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Fully Compatible
    • ALL
    • Hide
      > db.foo.drop()
      > db.foo.insert({x:ISODate()})
      > db.foo.aggregate({$project: {x:1, y: {$year: [{$add:['$x',1000]}]}}})
      {
      	"result" : [
      		{
      			"_id" : ObjectId("516341333512acfb2d33f156"),
      			"x" : ISODate("2013-04-08T22:14:11.665Z"),
      			"y" : 2013
      		}
      	],
      	"ok" : 1
      }
      > db.foo.aggregate({$project: {x:1, y: {$year: {$add:['$x',1000]}}}}))
      Error: Printing Stack Trace
          at printStackTrace (src/mongo/shell/utils.js:37:7)
          at DBCollection.aggregate (src/mongo/shell/collection.js:897:1)
          at (shell):1:8
      Mon Apr  8 18:15:15.198 JavaScript execution failed: aggregate failed: {
      	"errmsg" : "exception: the $year operator does not accept an object as an operand",
      	"code" : 16021,
      	"ok" : 0
      } at src/mongo/shell/collection.js:L898
      
      Show
      > db.foo.drop() > db.foo.insert({x:ISODate()}) > db.foo.aggregate({$project: {x:1, y: {$year: [{$add:[ '$x' ,1000]}]}}}) { "result" : [ { "_id" : ObjectId( "516341333512acfb2d33f156" ), "x" : ISODate( "2013-04-08T22:14:11.665Z" ), "y" : 2013 } ], "ok" : 1 } > db.foo.aggregate({$project: {x:1, y: {$year: {$add:[ '$x' ,1000]}}}})) Error: Printing Stack Trace at printStackTrace (src/mongo/shell/utils.js:37:7) at DBCollection.aggregate (src/mongo/shell/collection.js:897:1) at (shell):1:8 Mon Apr 8 18:15:15.198 JavaScript execution failed: aggregate failed: { "errmsg" : "exception: the $year operator does not accept an object as an operand" , "code" : 16021, "ok" : 0 } at src/mongo/shell/collection.js:L898

      The $year operator (and possibly others) throws an exception if the argument is an object (i.e. the result of a computation). Internally, it seems to be rejecting the argument due to legacy code for named arguments. Wrapping the object argument in an array bypasses the bug.

            Assignee:
            mathias@mongodb.com Mathias Stearn
            Reporter:
            jmikola@mongodb.com Jeremy Mikola
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: