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

查看:75
本文介绍了存在按字段聚合的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个则没有. 请注意,"field"的每个值都可以不同.我们只想对它的存在进行分组(或者非空对我也有用,我没有存储任何空值).

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天全站免登陆