mongodb Mongo 查询问题 $gt,$lt
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4966203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongo Query question $gt,$lt
提问by paullb
I have a query below. I want get items between 4 and 6 so only a:1 should match because it has the value 5 in b.
我在下面有一个查询。我想得到 4 到 6 之间的项目,所以只有 a:1 应该匹配,因为它在 b 中有值 5。
> db.test.find({ b : { $gt : 4 }, b: {$lt : 6}});
{ "_id" : ObjectId("4d54cff54364000000004331"), "a" : 1, "b" : [ 2, 3, 4, 5 ] }
{ "_id" : ObjectId("4d54d0074364000000004332"), "a" : 2, "b" : [ 2, 4, 6, 8 ] }
>
Can someone tell be why a:2 is matching this query? I can't really see why it is being returned.
有人能说出为什么 a:2 匹配这个查询吗?我真的不明白为什么它会被退回。
I also tried what was specified in the tutorial but id did not seem to work:
我还尝试了教程中指定的内容,但 id 似乎不起作用:
> db.test.find({ b : { $gt : 4, $lt : 6}});
{ "_id" : ObjectId("4d54cff54364000000004331"), "a" : 1, "b" : [ 2, 3, 4, 5 ] }
{ "_id" : ObjectId("4d54d0074364000000004332"), "a" : 2, "b" : [ 2, 4, 6, 8 ] }
>
And this one to avoid any confusion regarding GT/GTE
这个是为了避免任何关于 GT/GTE 的混淆
> db.test.find({b: {$gt: 4.5, $lt: 5.5}});
{ "_id" : ObjectId("4d54cff54364000000004331"), "a" : 1, "b" : [ 2, 3, 4, 5 ] }
{ "_id" : ObjectId("4d54d0074364000000004332"), "a" : 2, "b" : [ 2, 4, 6, 8 ] }
>
only a:1 should be returned.
只应返回 a:1。
As suggested, I gave $elemMatch a try but it did not appear to work either (objectIds are different because I am on a different machine)
按照建议,我尝试了 $elemMatch 但它似乎也不起作用(objectIds 不同,因为我在不同的机器上)
> db.test.find();
{ "_id" : ObjectId("4d5a24a5e82e00000000433f"), "a" : 1, "b" : [ 2, 3, 4, 5 ] }
{ "_id" : ObjectId("4d5a24bbe82e000000004340"), "a" : 2, "b" : [ 2, 4, 6, 8 ] }
> db.test.find({b: {$elemMatch: {$gt : 4, $lt: 6 }}});
>
No documents were returned.
没有文件被退回。
回答by jared
This is a really confusing topic. I work at 10gen and I had to spend a while wrapping my head around it ;)
这真是一个令人困惑的话题。我在 10gen 工作,我不得不花一些时间来解决它;)
Let's walk through how the query engine processes this query.
让我们来看看查询引擎如何处理这个查询。
Here's the query again:
再次查询:
> db.test.find({ b : { $gt : 4, $lt : 6}});
When it gets to the record that seems like it shouldn't match...
当它达到似乎不应该匹配的记录时......
{ "_id" : ObjectId("4d54cff54364000000004331"), "a" : 1, "b" : [ 2, 4, 6, 8 ] }
The match is not performed against each element of the array, but rather against the array as a whole.
匹配不是针对数组的每个元素执行,而是针对整个数组执行。
The comparison is performed in three steps:
比较分三步进行:
Step 1: Find all documents where b has a value greater than 4
步骤 1:查找 b 的值大于 4 的所有文档
b: [2,4,6,8] matches because 6 & 8 are greater than 4
b: [2,4,6,8] 匹配,因为 6 & 8 大于 4
Step 2: Find all documents where b has a value less than 6
第 2 步:查找 b 的值小于 6 的所有文档
b: [2,4,6,8] matches because 2 & 4 are less than 6
b: [2,4,6,8] 匹配,因为 2 & 4 小于 6
Step 3: Find the set of documents that matched in both step 1 & 2.
第 3 步:找到在第 1 步和第 2 步中都匹配的文档集。
The document with b: [2,4,6,8] matched both steps 1 & 2 so it is returned as a match. Note that results are also de-duplicated in this step, so the same document won't be returned twice.
带有 b: [2,4,6,8] 的文档与第 1 步和第 2 步都匹配,因此它作为匹配项返回。请注意,此步骤中也会对结果进行重复数据删除,因此不会两次返回相同的文档。
If you want your query to apply to the individual elements of the array, rather than the array as a whole, you can use the $elemMatch operator. For example
如果您希望查询应用于数组的单个元素,而不是整个数组,则可以使用 $elemMatch 运算符。例如
> db.temp.find({b: {$elemMatch: {$gt: 4, $lt: 5}}})
> db.temp.find({b: {$elemMatch: {$gte: 4, $lt: 5}}})
{ "_id" : ObjectId("4d558b6f4f0b1e2141b66660"), "b" : [ 2, 3, 4, 5, 6 ] }
回答by Jijo Paulose
$gt
$gt
Syntax: {field: {$gt: value} }
eg:
例如:
db.inventory.find( { qty: { $gt: 20 } } )
$lt
$lt
Syntax: {field: {$lt: value} }
eg:
例如:
db.inventory.find( { qty: { $lt: 20 } } )
eg2:
例2:
db.inventory.find({ qty : { $gt : 20, $lt : 60}});
回答by Anon
.find( {$and:[ {b:{$gt:4}}, {b:{$lt:6}} ]} )
回答by Andreas Jung
Because you did not check the documentation.
因为你没有检查文档。
See
看
http://www.mongodb.org/display/DOCS/Advanced+Queries
http://www.mongodb.org/display/DOCS/Advanced+Queries
and check for "ranges" on the page.
并检查页面上的“范围”。
Neither is your query syntax correct (compare against the example)
您的查询语法也不正确(与示例相比)
nor does your "why a:2" part of the question make any sense since 'a' is not involved in your query. If you want to search for a:1 then you have to include it in your query.
问题的“为什么 a:2”部分也没有任何意义,因为 'a' 不涉及您的查询。如果要搜索 a:1,则必须将其包含在查询中。
Keep in mind that all query clauses are AND combined by default unless you use the $or operator.
请记住,除非您使用 $or 运算符,否则默认情况下所有查询子句都是 AND 组合的。