2

我有一个带有“名称”索引的集合。

> db.search.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "name" : 1
        },
        "name" : "name_1"
    }
]
> 

我的一个疑问是

> db.collection.find({name: {$regex: '^string'}}).explain()

它使用索引

{
    "cursor" : "BtreeCursor name_1 multi",
    "isMultiKey" : false,
    "n" : 25,
    "nscannedObjects" : 25,
    "nscanned" : 3247,
    "nscannedObjectsAllPlans" : 25,
    "nscannedAllPlans" : 3247,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 366,
    "indexBounds" : {
        "name" : [
            [
                "",
                {

                }
            ],
            [
                /string/,
                /string/
            ]
        ]
    },
    "server" : "..."
}

我已经从带有索引字段的 MongoDB 正则表达式中知道,使用 BasiCursor 会更快。

> db.search.find({name: {$regex: 'string'}}).explain()
{
    "cursor" : "BasicCursor",
    "isMultiKey" : false,
    "n" : 27,
    "nscannedObjects" : 3385,
    "nscanned" : 3385,
    "nscannedObjectsAllPlans" : 3385,
    "nscannedAllPlans" : 3385,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 20,
    "indexBounds" : {

    },
}

如何告诉 Mongodb 在不删除索引的情况下使用 BasicCursor?

4

1 回答 1

3

hint您可以通过with强制查询优化器不使用任何索引$natural

> db.collection.find({name: {$regex: '^string'}}).hint({ $natural: 1})
于 2013-04-30T21:44:14.317 回答