mongodb - 为什么在字段上使用聚合函数 $avg 时 MongoDB 返回 null
问题描述
我不知道为什么这个查询只返回空值,字段中有整数,有些有空值但大多数有整数。我认为 $avg 忽略了空值。这是查询:
db.Companies.aggregate([
{$group:{_id:”$founded_year”,“avgNumEmployees”:{$avg:“$number_of_employees”}}},
{$redact:{$cond:{if:{
$eq:[“$avgNumEmployees”,“null”]},
then: “$$PRUNE”,
else:”$$KEEP”}
}}).pretty()
这是来自文档的一些示例数据
_id: 52cdef7c4bab8bd675297d8a
name:"Wetpaint"
permalink:"abc2"
crunchbase_url:
"http://www.crunchbase.com/company/wetpaint"
homepage_url: "http://wetpaint-inc.com"
blog_url:
"http://digitalquarters.net/"
blog_feed_url:
"http://digitalquarters.net/feed/"
twitter_username:"BachelrWetpaint"
category_code:"web"
number_of_employees:47
founded_year:2005
founded_month:10
founded_day:17
deadpooled_year:1
tag_list:
"wiki, seattle, elowitz, media-industry, media-platform, social-distrib..."
这是一个founded_year为null的:
_id:52cdef7c4bab8bd675297d92
name:"Flektor"
permalink:"flektor"
crunchbase_url:
"http://www.crunchbase.com/company/flektor"
homepage_url:
"http://www.flektor.com"
blog_url:
"http://www.flektor-blog.com"
blog_feed_url:
"http://www.flektor-blog.com/video_editing_software/index.rdf"
twitter_username:
null
category_code:
"games_video"
number_of_employees:null
founded_year:null
founded_month:null
founded_day:null
deadpooled_year: null
deadpooled_month: null
deadpooled_day: null
deadpooled_url: null
这些数据来自 mongoDB 的样本训练数据集,称为公司。
解决方案
推荐阅读
- spring-boot - ConditionalOnExpression 不适用于 FeignClient
- azure - 如何将 Azure Web App 与具有网关传输的 VNet 集成
- php - 如何找到缓存的 modx 片段?
- c - 这个 UTF8 到代码点的示例是否正确?
- r - 聚合字符行,拆分和重复
- php - 如何在pgsql中插入一个值,其中要插入的行等于函数中传递的数组数?
- python-3.x - 我在 Jupyter 笔记本中遇到 MemoryError,同时在数据框列上运行 pd.unique 等简单命令
- angular - Angular2数据表过滤列
- node.js - multer 中的 req.file 总是返回 undefined
- excel - VBA遍历文件夹中的文件并将变量范围复制/粘贴到主文件