禁止对查询

时间:2017-12-22 13:01:17

标签: mongodb optimization indexing

我有一个mongodb集合,其中包含以下架构:

{
    "description": "some arbitrary text",
    "type": "TYPE",  # there are a lot of different types
    "status": "STATUS"  # there are a few different statuses
}

我还有两个索引:“type”和“status”。

现在我运行一个查询:

db.obj.count({
    type: { $in: ["SOME_TYPE"] }, 
    status: { $ne: "SOME_STATUS" }, 
    description: { $regex: /.*/ } 
})

MongoDB选择使用“status”索引,而“type”会更好:

"query": {
    "count": "obj",
    "query": {
        "description": Regex('.*', 2),
        "status": {
            "$ne": "SOME_STATUS"
        },
        "type": {
            "$in": [
                "SOME_TYPE"
            ]
        }
    }
},
"planSummary": "IXSCAN { status: 1 }"

我知道我可以使用hint指定要使用的索引,但我有不同的查询(应该使用不同的索引),我无法对其中的每一个进行注释。

据我所知,一个可能的解决方案是禁止对包含status: { $ne: "SOME_STATUS" }条件的所有查询使用“status”索引。

有办法吗?或许我想要一些奇怪的东西并且有更好的方法?

0 个答案:

没有答案
相关问题