MongoDB按字段聚合存在

问题描述 投票:32回答:5

我很难相信这个问题还没有在某个地方被问过和回答,但是我找不到它的任何痕迹。

我有一个需要按布尔值分组的MongoDB聚合查询:是否存在另一个字段。

例如,让我们从这个集合开始:

> db.test.find()
{ "_id" : ObjectId("53fbede62827b89e4f86c12e"),
  "field" : ObjectId("53fbede62827b89e4f86c12d"), "name" : "Erik" }
{ "_id" : ObjectId("53fbee002827b89e4f86c12f"), "name" : "Erik" }
{ "_id" : ObjectId("53fbee092827b89e4f86c131"),
  "field" : ObjectId("53fbee092827b89e4f86c130"), "name" : "John" }
{ "_id" : ObjectId("53fbee122827b89e4f86c132"), "name" : "Ben" }

2个文件有“字段”,2个没有。注意,“field”的每个值可能不同;我们只想对它的存在进行分组(或者非null也适用于我,我没有存储任何空值)。

我已经尝试过使用$ project,但是$ exists不存在,$ cond和$ ifNull没有帮助我。该字段似乎总是存在,即使它不存在:

> db.test.aggregate(
  {$project:{fieldExists:{$cond:[{$eq:["$field", null]}, false, true]}}},
  {$group:{_id:"$fieldExists", count:{$sum:1}}}
)
{ "_id" : true, "count" : 4 }

我希望以下更简单的聚合工作,但由于某种原因,$ exists不支持这种方式:

> db.test.aggregate({$group:{_id:{$exists:"$field"}, count:{$sum:1}}})
assert: command failed: {
  "errmsg" : "exception: invalid operator '$exists'",
  "code" : 15999,
  "ok" : 0
} : aggregate failed
Error: command failed: {
  "errmsg" : "exception: invalid operator '$exists'",
  "code" : 15999,
  "ok" : 0
} : aggregate failed
    at Error (<anonymous>)
    at doassert (src/mongo/shell/assert.js:11:14)
    at Function.assert.commandWorked (src/mongo/shell/assert.js:244:5)
    at DBCollection.aggregate (src/mongo/shell/collection.js:1149:12)
    at (shell):1:9
2014-08-25T19:19:42.344-0700 Error: command failed: {
  "errmsg" : "exception: invalid operator '$exists'",
  "code" : 15999,
  "ok" : 0
} : aggregate failed at src/mongo/shell/assert.js:13

有谁知道如何从这样的集合中获得所需的结果?

预期结果:

{ "_id" : true, "count" : 2 }
{ "_id" : false, "count" : 2 }
mongodb aggregation-framework
5个回答
63
投票

我昨晚解决了同样的问题,这样:

> db.test.aggregate({$group:{_id:{$gt:["$field", null]}, count:{$sum:1}}})
{ "_id" : true, "count" : 2 }
{ "_id" : false, "count" : 2 }

有关其工作原理的完整说明,请参阅http://docs.mongodb.org/manual/reference/bson-types/#bson-types-comparison-order


10
投票

我通过检查未定义来解决它

$ne : [$var_to_check, undefined]

要么

$ne:  [ { $type : "$var_to_check"}, 'missing'] }

如果定义了var,则返回true


9
投票

$exists运算符是一个“查询”运算符,因此它基本上用于“过滤”结果而不是识别逻辑条件。

作为“逻辑”运算符,聚合框架支持$ifNull运算符。这将返回其存在的字段值或其未提供的备用提供值,或以其他方式计算为null

db.test.aggregate([
    { "$group": {
        "_id": { "$ifNull": [ "$field", false ] },
        "count": { "$sum": 1 }
    }}
])

但是,当然,即使这不是一个“真/假”的比较,所以除非你真的想要返回它所在的字段的实际值,那么你可能更喜欢使用$cond语句,就像你有:

db.test.aggregate([
    { "$group": {
        "_id": { "$cond": [{ "$eq": [ "$field", null ] }, true, false ] },
        "count": { "$sum": 1 }
    }}
])

$ifNull非常有用的地方在于替换不存在的数组字段,否则会导致使用$unwind出错。然后,您可以执行返回单个元素或空数组的操作,这样就不会在其余的管道处理中导致问题。


3
投票

Dunno怎么样但是现在2019年有清洁的解决方案。在聚合管道中执行此操作

$match: {"my_field": {$ne: null}}

好东西在我的郎'ne'意味着不是:)


1
投票

我的回答是:

{'$project': {
    'field_exists': {'$or': [
        {'$eq': ['$field', null]}, 
        {'$gt': ['$field', null]},
    ]},
}}

这是详细信息。 $ exists表示该字段存在,即使它是null或任何其他空值。这就是为什么这个页面上的所有答案都不正确的原因。

我们来试试吧。检查一下:

// Let's take any collection that have docs
db.getCollection('collection').aggregate([
  // Get arbitrary doc, no matter which, we won't use it
  {"$limit": 1},
  // Project our own fields (just create them with $literal)
  {'$project': {
    '_id': 0,
    'null_field': {'$literal': null},
    'not_null_field': {'$literal': {}},
  }},
])

我们会得到这个:

{
    "null_field" : null,
    "not_null_field" : {}
}

然后让我们澄清一下这个doc中存在哪些字段:

  1. null_field - 存在
  2. not_null_field - 存在
  3. non_existent_field - 没有。

好的,是时候测试我上面提到的项目阶段了。让我们为我们感兴趣的每个领域添加它:

{'$project': {
    'null_field_exists': {'$or': [
        {'$eq': ['$null_field', null]}, 
        {'$gt': ['$null_field', null]},
    ]},
    'not_null_field_exists': {'$or': [
        {'$eq': ['$not_null_field', null]}, 
        {'$gt': ['$not_null_field', null]},
    ]},
    'non_existent_field_exists': {'$or': [
        {'$eq': ['$non_existent_field', null]}, 
        {'$gt': ['$non_existent_field', null]},
    ]},
}},

我们得到的是:

{
    "null_field_exists" : true,
    "not_null_field_exists" : true,
    "non_existent_field_exists" : false
}

正确!

还有一个小注意事项:我们使用null进行比较,因为它是最小值,至少是有价值的(较小的只是不存在)。

© www.soinside.com 2019 - 2024. All rights reserved.