mongoose 聚合 $avg $ 或返回空值

Posted

技术标签:

【中文标题】mongoose 聚合 $avg $ 或返回空值【英文标题】:mongoose aggregation $avg $or returns null value 【发布时间】:2016-11-16 17:41:35 【问题描述】:

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

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 

为了证明这一点,我运行了这个聚合

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】:

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

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

【讨论】:

以上是关于mongoose 聚合 $avg $ 或返回空值的主要内容,如果未能解决你的问题,请参考以下文章

SqlServer函数的聚合函数

7-07聚合函数

聚合函数数据分组

sql聚合函数的应用

11汇总和分组数据

SQL中只要用到聚合函数就一定要用到group by 吗?