mongoose聚合$ avg $或返回null值

时间:2016-07-13 23:49:57

标签: mongodb mongoose group-by aggregation-framework or-operator

我想计算日期之间的平均测量值。 这是聚合的代码

db.measurements.aggregate([{
  $match: {
    $or: [{
      date: {
        '$gte': ISODate('2016-06-01T22:52:46Z'),
        '$lte': ISODate('2016-06-02T22:52:46Z')
      }
    }]
  }
}, {
  $group: {
    _id: "$workplace",
    avgTemperature: {
      $avg: "$temperature"
    }
  }
}]);

它返回第一个具有空值但它不可能具有空均值

{ "_id" : ObjectId("5761df5981fb3799edbeda88"), "avgTemperature" : null }
{ "_id" : ObjectId("5761d3c681fb3799edbeda83"), "avgTemperature" : 16.323529411764707 }

要证明我运行这种aggegration

db.measurements.aggregate([{
  $match: {
     workplace: {
        $in: [ObjectId("5761df5981fb3799edbeda88")]
      },
    $or: [{
      date: {
        '$gte': ISODate('2016-06-01T22:52:46Z'),
        '$lte': ISODate('2016-06-02T22:52:46Z')
      }
    }]
  }
}]);

响应数据

{ "_id" : ObjectId("576f1a7ab1a9cb96dbf74773"), "date" : ISODate("2016-06-02T11:28:56Z"), "temperature" : "11.6", "noise" : "19.0", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf74785"), "date" : ISODate("2016-06-02T03:49:59Z"), "temperature" : "12.2", "noise" : "17.7", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf74795"), "date" : ISODate("2016-06-02T01:39:06Z"), "temperature" : "12.6", "noise" : "55.6", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf74797"), "date" : ISODate("2016-06-02T04:07:47Z"), "temperature" : "11.8", "noise" : "21.5", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf747d8"), "date" : ISODate("2016-06-02T10:30:24Z"), "temperature" : "6.4", "noise" : "47.6", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf7480b"), "date" : ISODate("2016-06-02T02:12:27Z"), "temperature" : "12.2", "noise" : "39.3", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf74811"), "date" : ISODate("2016-06-02T10:01:12Z"), "temperature" : "23.8", "noise" : "29.3", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf74816"), "date" : ISODate("2016-06-02T22:42:44Z"), "temperature" : "3.8", "noise" : "5.7", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf74889"), "date" : ISODate("2016-06-02T17:16:12Z"), "temperature" : "4.3", "noise" : "67.4", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf7488c"), "date" : ISODate("2016-06-02T01:59:37Z"), "temperature" : "24.4", "noise" : "55.2", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf74899"), "date" : ISODate("2016-06-02T22:31:13Z"), "temperature" : "9.5", "noise" : "66.4", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf748a9"), "date" : ISODate("2016-06-02T15:44:03Z"), "temperature" : "24.8", "noise" : "54.5", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf748af"), "date" : ISODate("2016-06-02T16:46:51Z"), "temperature" : "2.7", "noise" : "14.8", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf748d6"), "date" : ISODate("2016-06-02T19:37:01Z"), "temperature" : "13.0", "noise" : "22.2", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf748f2"), "date" : ISODate("2016-06-02T07:30:30Z"), "temperature" : "5.7", "noise" : "16.9", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf74912"), "date" : ISODate("2016-06-02T17:28:55Z"), "temperature" : "10.9", "noise" : "13.0", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf74940"), "date" : ISODate("2016-06-02T12:07:37Z"), "temperature" : "18.4", "noise" : "64.4", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf74946"), "date" : ISODate("2016-06-02T19:14:08Z"), "temperature" : "12.4", "noise" : "69.5", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf74950"), "date" : ISODate("2016-06-02T22:19:55Z"), "temperature" : "16.3", "noise" : "22.4", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
{ "_id" : ObjectId("576f1a7ab1a9cb96dbf7495d"), "date" : ISODate("2016-06-02T06:23:00Z"), "temperature" : "16.2", "noise" : "51.8", "workplace" : ObjectId("5761df5981fb3799edbeda88") }
Type "it" for more

如何修复它来计算平均值而不会出现问题。或者代码是否正常,问题与代码无关?因为我确实导入了模拟数据...

1 个答案:

答案 0 :(得分:1)

据我所知,aggregate无法更改现有数据类型,$avg可以使用数值(例如:int,long,float,double,number)。目前,MongoDB没有运算符将字符串解析为数字。

因此,您需要将String的架构Number转换为temperature字段并编写脚本,将字符串的数据库temperature值转换为数字,然后再转换为代码将返回完美的结果。你的查询很完美。