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

Add Aggregation Framework expression to average dates

    • Query Execution

      Add an accumulator/expression that takes dates and returns their average (could be called $avgDate).

      Original description:

      When using aggregation framework $avg on an ISODate in mongo shell I get the following error message:

      {
      	"errmsg" : "exception: can't convert from BSON type 2 to double",
      	"code" : 16005,
      	"ok" : 0
      }
      

      Here's my setup. Collection data:

      > db.sys_user.find({}, {sys_created_on:1}).limit(3)
      { "_id" : ObjectId("502a95d4c2e6f4d8ffbbef21"), "sys_created_on" : ISODate("2012-02-18T03:04:49Z") }
      { "_id" : ObjectId("502a95d4c2e6f4d8ffbbef22"), "sys_created_on" : ISODate("2012-02-18T03:04:49Z") }
      { "_id" : ObjectId("502a95d4c2e6f4d8ffbbef23"), "sys_created_on" : ISODate("2012-02-18T03:04:49Z") }
      

      This errors:

      > db.sys_user.aggregate([ { $group: {_id:null, max:{$max:"$sys_created_on"}, min:{$min:"$sys_created_on"}, avg:{$avg:"sys_created_on"}} }, { "$project" : { "_id" : 0 , "max": 1, "min": 1, "avg": 1}} ])
      {
      	"errmsg" : "exception: can't convert from BSON type 2 to double",
      	"code" : 16005,
      	"ok" : 0
      }
      

      $min, $max work fine:

      > db.sys_user.aggregate([ { $group: {_id:null, max:{$max:"$sys_created_on"}, min:{$min:"$sys_created_on"}} }, { "$project" : { "_id" : 0 , "max": 1, "min": 1}} ])
      {
      	"result" : [
      		{
      			"max" : ISODate("2012-03-21T05:13:21Z"),
      			"min" : ISODate("2004-05-02T00:00:00Z")
      		}
      	],
      	"ok" : 1
      }
      

            Assignee:
            backlog-query-execution [DO NOT USE] Backlog - Query Execution
            Reporter:
            joe.davis@servicenow.com Joe Davis
            Votes:
            2 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: