MongoDB中查询到文档数组的每个元素相匹配的条件 [英] mongodb query to match each element in doc array to a condition

查看:253
本文介绍了MongoDB中查询到文档数组的每个元素相匹配的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须把这个文档类似:​​

I have docs analogous to this:

{_ ID:1,值:[2,3,4]}

{_id: 1, values : [2,3,4] }

{_编号:2,值:[4]}

{_id: 2, values: [4] }

{_ ID:3,值:[3,4,5,6,7,8,9,10,11]}

{_id: 3, values : [3,4,5,6,7,8,9,10,11] }

,其中每个文档具有一个数组。我需要一个查询,只有当它的阵列中的每个元素匹配所希望的标准(如果任何元素相匹配,而不是)返回该文档。

in which each doc has an array. I need a query that only returns the doc if EACH element of its array match the desired criteria (rather than if ANY element matches).

例如。类似(但不是)

{'值':{'$ GT':1,'$ LT':5}})

{ 'values' : { '$gt' : 1, '$lt': 5} })

这将成功地返回前两个,但不是第三文档,因为不是所有的
第三文档的数组价值的元素相匹配的标准。

which would successfully return the first two but not the third doc, as not all elements of the third doc's array 'values' match the criteria.

显然MongoDB的阵列上使用隐式或查询,而我需要的。

Apparently mongodb uses an implicit OR in queries on arrays, whereas I need AND.

我想我可以手动指数每一个元素,例如:

I guess I could manually index each element, eg.:

collection.find({values​​.0:{$ GT:1,$ LT:5},values​​.1:{$ GT:1,$ LT:5},... values​​.n:{$ GT :1,$ LT:5}}),但是这是我的高动态数组痛苦。

collection.find({values.0: {$gt:1,$lt:5}, values.1:{$gt:1,$lt:5}, ... values.n:{$gt:1,$lt:5}}) but this is a pain with my highly dynamic arrays.

有没有更好的办法?

注:我在问的MongoDB用户的这种过度但作为新的与$所有运营商的MongoDB产生混淆。在这里,我关心的文档数组,而不是一个查询数组。此外,在这种情况下,数字我意识到有人可能会编写所需否定范围的查询,但总的来说,我就不能写否定。

Note: I asked this over at mongodb-user but being new to mongodb generated confusion with $all operator. Here I am concerned about the doc array, not a query array. Also, in this numeric case I realize one might write a query that negates the range desired, but in general I won't be able to write the negation.

推荐答案

我不认为有任何的方式来通过您的文档手动遍历和检查数组中的每个值做到这一点的是,分开。这将是相当缓慢的,因为它在每个文件执行JavaScript,而不能利用任何索引超过 col.values​​

I don't think there's any way to do this yet, apart from manually iterating through your documents and checking each value in the array. That's going to be quite slow because it has to execute JavaScript on each document, and can't take advantage of any index over col.values.

<击>即使<一个href=\"http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-JavascriptEx$p$pssionsand%7B%7B%24where%7D%7D\">$where JavaScript的前pression查询似乎并不在这里工作,因为这可能是因为查询包含回调和太复杂:

Even a $where JavaScript expression query doesn't seem work here because, possibly because the query contains a callback and is too complex:

db.col.find("this.values.every(function(v) { return (v > 1 && v < 5) })")

编辑:对于某些查询,包括这一项,JavaScript的<一个href=\"http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-JavascriptEx$p$pssionsand%7B%7B%24where%7D%7D\">$where前pression需要一个return语句,所以这个工作得很好:

For some queries, including this one, the JavaScript $where expression needs a return statement, so this works fine:

db.col.find("return this.values.every(function(v) { return (v > 1 && v < 5) })")

这篇关于MongoDB中查询到文档数组的每个元素相匹配的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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