MongoDB-降序索引怪异行为 [英] MongoDB - descending index weird behavior

查看:189
本文介绍了MongoDB-降序索引怪异行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的test数据库中有这个test集合.

I have this test collection in my test database.

[test] 2014-02-11 18:01:33.970 >>> db.test.find ();
{ "_id" : ObjectId("52faa8a695fa10cc7d2b7908"), "x" : 1 }
{ "_id" : ObjectId("52faa8ab95fa10cc7d2b7909"), "x" : 5 }
{ "_id" : ObjectId("52faa8ad95fa10cc7d2b790a"), "x" : 15 }
{ "_id" : ObjectId("52faa8b095fa10cc7d2b790b"), "x" : 25 }
{ "_id" : ObjectId("52faa8b795fa10cc7d2b790c"), "x" : [ 5, 25 ] }

如果我使用

[test] 2014-02-11 18:01:50.615 >>> db.test.createIndex({x : 1});

然后此查找查询运行正常,并返回预期结果:

then this find query is working fine and returning the expected result:

[test] 2014-02-11 18:01:48.72 >>> db.test.find ( { x : {$gt: 10, $lt: 20} } ).min({x:10}).max({x:20});
{ "_id" : ObjectId("52faa8ad95fa10cc7d2b790a"), "x" : 15 }
[test] 2014-02-11 18:01:50.615 >>>

如果删除该升序索引,然后用

Buf if drop that ascending index, and then create a descending index with

[test] 2014-02-11 18:01:50.615 >>> db.test.createIndex({x : -1});

然后相同的查找查询返回此错误:

then the same find query is returning this error:

error: {
        "$err" : "no index found for specified keyPattern: {} min: { x: 10.0 } max: { x: 20.0 }",
        "code" : 10367
}

我期望MongoDB在这里返回相同的文档:

I was expecting that MongoDB would return the same document here:

{ "_id" : ObjectId("52faa8ad95fa10cc7d2b790a"), "x" : 15 }      

这对我来说听起来不合逻辑.
这是为什么?为什么MongoDB无法在x上使用该降序索引?

This sounds illogical to me.
Why is that? Why is MongoDB not able to use that descending index on x?

创建索引
放置索引
获取索引

create index
drop index
get indexes

推荐答案

这对我有用:

> db.h.insert({x:15})
> db.h.createIndex({x:-1})
> db.h.find().min({x:20}).max({x:10})
{ "_id" : ObjectId("52fb6930253ac3dcf43b27f5"), "x" : 15 }

您的索引可能有问题.

之所以可行,是因为索引是相反的.

The reason this works is because the index is the other way around.

想象一下,您将一个列表翻了个头,最小和最大有效表示的是获取该列表的范围,最小为10,最大为20.但是,由于该列表上下颠倒,因此该范围不再存在.相反,必须将范围取反以匹配列表.

Imagine you turn a list on its head, what min and max effectively say is get a range of that list with the min being 10 and max being 20. However that range no longer exists since the list is upside down. Instead the range must be reversed to match the list.

这篇关于MongoDB-降序索引怪异行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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