所以我有一个文件的这种结构:MongoDB的:子文档数组的索引
{
_id: "123abc",
mainProps: [
{
"countrycode": "US"
},
{
"yearfounded": "2011"
},
{
"city": "New York"
},
...
],
otherProps: [{}, {}, ...]
}
我有一个索引设置是这样的:
db.companies.ensureIndex({mainProps: 1})
的任务是创建一个Web窗体中搜索这些文件。表单中的字段不是固定的,可以添加。基本上我不知道用户想要过滤哪些字段,所以我不能设置适当的复合索引。数据库将超过20mil文件,现在大约10mil。
问题是我的索引不起作用,或者工作不正确。 查看一些示例。
此查询根本没有索引。
db.companies.find({'mainProps.yearfounded': '2012'}).explain()
此查询使用索引,并罚款。
db.companies.find({mainProps:{'yearfounded': '2012'}}).explain()
而且这样的挂起(如果我删除的解释()),我不知道是否它执行或正在发生的事情。
db.companies.find(
{$or: [
{ mainProps: {foundedyear: '2012'}},
{ mainProps: {foundedyear: '2011'}},
]}
).explain()
对于最后一个查询解释我得到了这样的东西。
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "leadsbase.companies",
"indexFilterSet" : false,
"parsedQuery" : {
"$or" : [
{
"mainProps" : {
"$eq" : {
"foundedyear" : "2012"
}
}
},
{
"mainProps" : {
"$eq" : {
"foundedyear" : "2011"
}
}
}
]
},
"winningPlan" : {
"stage" : "SUBPLAN",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"mainProps" : 1
},
"indexName" : "mainProps_1",
"isMultiKey" : true,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"mainProps" : [
"[{ foundedyear: \"2011\
" }, { foundedyear: \"2011\" }]",
"[{ foundedyear: \"2012\
" }, { foundedyear: \"2012\" }]"
]
}
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "vm1",
"port" : 27017,
"version" : "3.2.8",
"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbag0"
},
"ok" : 1
}
所以据我了解索引是存在的,但由于某种原因不能正常工作。
我应该如何构建我的领域,或者我应该如何为此设置索引?
为什么你说最后一个查询不使用索引扫描,当这个winsplan说要使用IXSCAN? – vdj4y
>“据我了解,索引是存在的,但由于某种原因不起作用。” 我不太明白发生了什么事,但是如果没有“解释”就执行此操作需要花费很长时间,即使我将“查找”更改为“计数” – Tooyz