mongoid 索引仍然导致 COLLSCAN

mongoid index still resulting in a COLLSCAN

我有一个简单的 class 和一个简单的索引

class A
    include Mongoid::Document

    field :value
    index({value: 1})

在命令行,我重新索引

bundle exec rake db:mongoid:remove_indexes
bundle exec rake db:mongoid:create_indexes

但是当我 运行 在 rails 控制台中进行简单查询时

A.where(value: "1").to_a

mongodb 日志清楚地显示了一个 COLLSCAN

command test_development.a command: find { find: "a", filter: { value: "1" }, 
$db: "test_development", lsid: { id: UUID("fa470127-398a-4f06-9a17-57b058017cf7") } }   
planSummary: COLLSCAN keysExamined:0 docsExamined:816688 cursorExhausted:1 
numYields:6380 nreturned:0 

我做错了什么?

更新 - 添加了附加信息:

db.case_data.getIndexSpecs()
[
    {
            "v" : 2,
            "key" : {
                    "_id" : 1
            },
            "name" : "_id_",
            "ns" : "simple_development.case_data"
    },
    {
            "v" : 2,
            "key" : {
                    "_fts" : "text",
                    "_ftsx" : 1
            },
            "name" : "value_text",
            "ns" : "simple_development.case_data",
            "weights" : {
                    "value" : 1
            },
            "default_language" : "english",
            "language_override" : "language",
            "textIndexVersion" : 3
    }
]

解释的输出:

db.case_data.find({value: "test"}).explain()
{
    "queryPlanner" : {
            "plannerVersion" : 1,
            "namespace" : "simple_development.case_data",
            "indexFilterSet" : false,
            "parsedQuery" : {
                    "value" : {
                            "$eq" : "test"
                    }
            },
            "queryHash" : "7E4E9C25",
            "planCacheKey" : "7E4E9C25",
            "winningPlan" : {
                    "stage" : "COLLSCAN",
                    "filter" : {
                            "value" : {
                                    "$eq" : "test"
                            }
                    },
                    "direction" : "forward"
            },
            "rejectedPlans" : [ ]
    },
    "serverInfo" : {
            "host" : "msc-2",
            "port" : 27017,
            "version" : "4.2.8",
            "gitVersion" : "43d25964249164d76d5e04dd6cf38f6111e21f5f"
    },
    "ok" : 1
}

我不知道为什么要这样创建索引,可能是因为你没有在模型中指定字段的类型class。尝试指定字段的类型,然后在 mongo shell db.getCollection('X').dropIndex('value_text') 中手动删除索引,然后 运行 创建索引的抽成 rake db:create_indexes.

如果这不起作用,我会删除索引并在 mongo shell db.getCollection('X').createIndex({'value': 1}, {name: 'value_1', background: true}) 中手动创建一个(或等待更好的答案)

在 Mongoid 7.1.1 和 mongodb 4.2.9 的某些组合中,rake 任务和 mongorestore 两者实际上都不会重新创建索引。他们说他们做,但他们没有。

此命令生成 COLLSCAN:

bundle exec rake db:mongoid:remove_indexes
bundle exec rake db:mongoid:create_indexes

使用 mongo 控制台创建索引,或者在每个模型中,在 Rails 控制台中,执行:

A.remove_indexes
A.create_indexes

没有生成 COLLSCAN。

我已经用干净的数据库恢复科学地重复了这一点,并且可以肯定地说 rake 命令和 mongorestore 命令不会重新创建索引。我上面的解决方案解决了这个问题。

感谢大家的帮助, 凯文