MongoDB:如何将$ size的数组与另一个文档项进行比较? [英] MongoDB: how to compare $size of array to another document item?

查看:109
本文介绍了MongoDB:如何将$ size的数组与另一个文档项进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MongoDB:如何做这样的事情,无论是在mongo控制台还是通过JavaScript,Node.js:

MongoDB: how to do something like this, both in mongo console and via JavaScript, Node.js:

db.turnys.find( { users:{$size:seats } } )

turnydb.turnys看起来像这样:

turnydb.turnys looks like this:

[
  {
    "gId": "5335e4a7b8cf51bcd054b423",
    "seats": 2,
    "start": "2014-03-30T14:23:29.688Z",
    "end": "2014-03-30T14:25:29.688Z",
    "rMin": 800,
    "rMax": 900,
    "users": [],
    "_id": "533828e1d1a483b7fd8a707a"
  },
  {
    "gId": "5335e4a7b8cf51bcd054b423",
    "seats": 2,
    "start": "2014-03-30T14:23:29.688Z",
    "end": "2014-03-30T14:25:29.688Z",
    "rMin": 900,
    "rMax": 1000,
    "users": [],
    "_id": "533828e1d1a483b7fd8a707b"
  },
...
]


推荐答案

接受使用真正的问题docs.mongodb.org/manual/reference/operator/query/where/rel =nofollow noreferrer> $ where 运营商以任何的方式运营。特别是考虑到你提出的一些后期问题。您需要阅读此运营商的手册页(如链接中所示)。

There are real problems with the acceptance of using the $where operator in any sort of way. Especially considering some of your "later" questions that have been raised. You need to read the manual page on this operator (as given in the link).

短点:


  • $ where 运营商调用完整集合扫描为了做到这一点的比较。这是真实世界案例中的错误新闻,因为无法约束此搜索以任何方式使用索引,从而减少工作集。

  • The $where operator invokes a full collection scan in order to do it's comparisons. This is bad news in "real world" cases since there is no way to "constrain" this search to use an index in any way, and thus reduce the working set.

本质上,此运算符调用任意JavaScript执行。这意味着两件事。 1 。)每个文档必须从其本机BSON表单扩展为JavaScript对象表单(成本),以进行每次比较。 2 。)您正在调用JavaScript解释器,即本机代码,每个每次比较。

By nature, this operator invokes "arbitrary JavaScript execution". And this means two things. 1.) Every document must be expanded from it's native BSON form into the "JavaScript" object form (at a cost) for each comparison. 2.) You are invoking a "JavaScript" interpreter, that is not native code, for each and every comparison.

一个 答案为您提供了有效警告,但它没有为您提供实际答案。所以这里是:

One of the answers gave you valid warnings on this, though it did not provide you with an actual answer. So here it is:

如上所述,应该实际上保持计数的数组文档中的元素,如果这对您很重要。但是,虽然您可能已将此视为有效,但正确比较完成如下。

As stated, yes you should actually be keeping a "count" of the "array" elements within your document if this is important to you. But whilst you may have seen this as valid, the proper comparison is done as follows.

因此,我们假设您确实使用了 $ inc 建议在文档中保留 total_users 字段。

So let us assume you did use $inc as suggested to keep a total_users field in your document.

db.collection.aggregate([
    { "$project": {
        "gId": 1,
        "alloc": { "$eq": [ "$total_users", "$seats" ] }
    }},
    { "$match": { "alloc": 1 } }
])

基本上你可以 $ project 文档中所需的所有字段,只要您使用alloc字段进行比较,只需执行逻辑比较,如 $ eq 运营商。

Where essentially you can $project all the fields you want in the document, just as long as you make the comparison as shown with the "alloc" field by simply doing a "logical" comparison as shown with the $eq operator.

在未来的版本中(很快)在写作时)你甚至可以找到 <$ c 聚合操作中数组的$ c> $ size 。所以而不是在数组成员上保留一个计数器,你可以这样做:

In future releases ( very soon as of writing ) you even get to find out the $size of an array within the "aggregation" operation. So "instead" of keeping a counter on the "array" members you can do this:

db.collection.aggregate([
    { "$project": {
        "gId": 1,
        "alloc": { "$eq": [ { "$size": "$users" }, "$seats" ] }
    }},
    { "$match": { "alloc": 1 } }
])

但自然会带来成本,所以仍然最好选项是根据您的需要在文档上保留计数器。

But naturally that will come with a "cost", so still the best option is to keep that "counter" on your document, depending on your needs.

虽然两个方法在这里仍然*导致收集扫描(在此上下文中,因为没有其他$匹配条件),最终结果将比显示的JavaScript执行快很多倍在其他答案中。

While both approaches here do *still** result in a "collection scan" (in this context since there was no other $match condition), the end result will be many times faster than the "JavaScript" execution that was shown in other answers.

使用本机代码方法作为最佳实践解决方案。

Use the "native code" methods as a best practice solution.

这篇关于MongoDB:如何将$ size的数组与另一个文档项进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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