MongoDB 按字段聚合存在 [英] MongoDB aggregate by field exists

查看:12
本文介绍了MongoDB 按字段聚合存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a hard time believing this question hasn't been asked and answered somewhere already, but I can't find any trace of it.

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

I have a MongoDB aggregation query that needs to group by a boolean: the existence of another field.

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

For example let's start with this collection:

> 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 个没有.请注意,字段"的每个值可能不同;我们只想对它的存在进行分组(或者非空值也适用于我,我没有存储任何空值).

2 documents have "field", and 2 don't. Note that each value of "field" may be different; we just want to group on its existence (or non-nullness works for me too, I don't have any null values stored).

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

I've tried using $project, but $exists doesn't exist there, and $cond and $ifNull haven't helped me. The field always appears to exist, even when it doesn't:

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

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

I would expect the following much simpler aggregate to work, but for some reason $exists isn't supported in this way:

> 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

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

Does anyone know how to get the desired result from a collection like this?

预期结果:

{ "_id" : true, "count" : 2 }
{ "_id" : false, "count" : 2 }

推荐答案

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

I solved the same problem just last night, this way:

> 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 了解其工作原理的完整说明.

See http://docs.mongodb.org/manual/reference/bson-types/#bson-types-comparison-order for a full explanation of how this works.

这篇关于MongoDB 按字段聚合存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆