-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.2.0-rc0, 2.2.0-rc1
-
Component/s: Aggregation Framework
-
Environment:Linux, Mac OSX
-
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 }