mongodb查询:$ size和$ gt始终返回0 [英] mongodb query: $size with $gt returns always 0

查看:327
本文介绍了mongodb查询:$ size和$ gt始终返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在mongodb查询中启用不相等的数组大小.

I wonder what can I do to enable inequalities on the size of arrays in mongodb queries.

例如,我有以下两个查询:

For example, I have the following two queries:

> db.userStats.count({sessions:{$size:1}})
1381
> db.userStats.count({sessions:{$size:{$gt:0}}})
0

如果$gt按我的预期工作,则第二次查询的结果将有所不同,导致得出的数字至少为1381.

If $gt worked as I intend the result would be different for the second query, leading to a number that is at least 1381.

第二个查询出了什么问题,如何解决它以获得我想要的结果?

What is wrong with the second query and how can I fix it to achieve the result I want?

谢谢

推荐答案

您不能执行> 文档中实际引用的操作$size .因此,该运算符将单独评估文字"结果,并且不能与您要求的范围运算符"结合使用.

You cannot do that as is actually referenced in the documentation for $size. So that operator alone evaluates a "literal" result and cannot be use in conjunction with "range operators" such as you are asking.

您唯一的实际选择是通过使用

Your only real options are to ask for a "size" that is "not equal to 0" by negating the condition with the $not operator. Which is perfectly valid:

db.userStats.count({ "sessions": { "$not": { "$size": 0 } } });

或者以其他方式测试 $size (只要您的MongoDB版本为2.6或更高版本):

Or otherwise test with the other incarnation of $size in the aggregation framework, as long as your MongoDB version is 2.6 or greater:

db.userStats.aggregate([
    { "$group": {
         "_id": null,
         "count": {
             "$sum": {
                 "$cond": [
                     { "$gt": [ { "$size": "$sessions", 0 } ] },
                     1,
                     0
                 ]
             }
         }
    }}
])

可能还使用 $where JavaScript评估形式:

Possibly also with the $where form of JavaScript evaluation:

db.userStats.count(function() { return this.sessions.length > 0 });

但可能比上一个版本慢.

But likely slower than the last version.

或者实际上,您可以使用点表示法" $exists 运算符:

Or in fact you can just do this with "dot notation", and the $exists operator:

db.userStats.count({ "sesssions.0": { "$exists": true } });

通常的想法是,如果索引0处有一个元素,则数组具有一定的长度.

As the general idea is that if there is an element at the index 0 then the array has some length.

这完全取决于您的方法,但是这些形式中的任何一种都可以得到正确的结果.

It all depends on your approach, but any of these forms gets the right result.

但要获得MongoDB的最佳"性能,请不要使用这些方法中的任何.而是将数组"length"保留为要检查的文档的属性.您可以对其进行索引",并且发出的查询实际上可以访问该索引,而上述任何一项都无法做到.

But for the "best" performance from MongoDB, don't use any of these methods. Instead, keep the array "length" as a property of the document you are inspecting. This you can "index" and the queries issued can actually access that index, which none of the above can do.

像这样维护它:

db.userStats.update(
     { "_id": docId, "sessions": { "$ne": newItem } },
     {
         "$push": { "sessions": newItem },
         "$inc": { "countSessions": 1 }
     }
)

或删除:

db.userStats.update(
     { "_id": docId, "sessions": newItem  },
     {
         "$pull": { "sessions": newItem },
         "$inc": { "countSessions": -1 }
     }
)

然后,您可以仅查询"countSesssions",该索引也可以索引以获得最佳性能:

Then you can just query on "countSesssions" which can also index for best performance:

db.userStats.find({ "countSessions": { "$gt": 0 } })

那是做到这一点的最佳"方法.

And that is the "best" way to do it.

这篇关于mongodb查询:$ size和$ gt始终返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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