Mongo查询问题$ gt,$ lt [英] Mongo Query question $gt,$lt

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

问题描述

我在下面有一个查询.我想获取4到6之间的项目,所以只有a:1应该匹配,因为它在b中的值为5.

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.

> 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 ] }
>

有人可以说出为什么a:2匹配此查询吗?我真的看不出来为什么要退货.

Can someone tell be why a:2 is matching this query? I can't really see why it is being returned.

我也尝试了本教程中指定的内容,但id似乎不起作用:

I also tried what was specified in the tutorial but id did not seem to work:

> 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 ] }
>

这是为了避免对GT/GTE造成任何混淆

And this one to avoid any confusion regarding 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 ] }
>

仅应返回a:1.

如建议的那样,我尝试了$ elemMatch,但是它似乎也不起作用(objectIds不同,因为我在另一台机器上)

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)

> 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 }}});
>

没有退回任何文件.

推荐答案

这是一个非常令人困惑的话题.我在10gen上班,不得不花点时间在上面;)

This is a really confusing topic. I work at 10gen and I had to spend a while wrapping my head around it ;)

让我们逐步了解查询引擎如何处理此查询.

Let's walk through how the query engine processes this query.

再次是查询:

> 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:

步骤1 :查找b的值大于4的所有文档

Step 1: Find all documents where b has a value greater than 4

b:[2,4,6,8]匹配,因为6& 8大于4

b: [2,4,6,8] matches because 6 & 8 are greater than 4

第2步:查找b的值小于6的所有文档

Step 2: Find all documents where b has a value less than 6

b:[2,4,6,8]匹配是因为2& 4小于6

b: [2,4,6,8] matches because 2 & 4 are less than 6

步骤3 :查找在步骤1和步骤2中都匹配的文档集. 2.

Step 3: Find the set of documents that matched in both step 1 & 2.

带有b:[2,4,6,8]的文档匹配步骤1和步骤2. 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.

如果希望查询应用于数组的单个元素,而不是整个数组,则可以使用$ elemMatch运算符.例如

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

> 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 ] }

这篇关于Mongo查询问题$ gt,$ lt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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