有次同事在MongoDB中执行一个简单查询的时候,出现了如下错误:
rs1:PRIMARY> db.songs.find({'lyrics': ''})
error: {
"$err" : "Unable to execute query: error processing query: ns=lyrics.songs limit=0 skip=0\nTree: lyrics == \"\"\nSort: {}\nProj: {}\n No query solutions",
"code" : 17007
}
查询语句在语法上没有什么问题,但是从错误提示来看,是因为没有query solutions. 这个问题第一次遇到,经过在Google上面搜索发现有人在MongoDB JIRA上面提了一个相关的ISSUE:The $exists operator fails if the field is not indexed when using --notablescan. 看来与notablescan这个选项有关系。在这个ISSUE下面,有人作了如下解释:
As you suspect, the "no query solutions" error message happens because 1) there are no indexed solutions available for the $exists query, and 2) collection scans are disallowed by the notablescan option. Therefore, I'm resolving this issue as "Works as Designed, Backwards Breaking". Thanks for pointing out the behavior change between 2.4 versions and 2.6.0--I'm going to make sure that this gets properly documented by our docs team. Do let us know if you find anything else surprising about 2.6.0!
出现“no query solutions”的原因是查询没有索引可以利用并且全表扫描被禁止。的确,在文首那个出错的查询中,lyrics字段没有索引,所以会导致全表扫描。而开启了notablescan选项后,全表扫描是不允许的。因此我们怀疑服务器开启了notablescan.
可以验证一下:
rs1:PRIMARY> use test;
switched to db test
rs1:PRIMARY> db.test.insert({})
WriteResult({ "nInserted" : 1 })
rs1:PRIMARY> db.test.find()
{ "_id" : ObjectId("57086da1506457408b6ef875") }
rs1:PRIMARY> db.test.find({'not_exist_field': ''})
error: {
"$err" : "Unable to execute query: error processing query: ns=test.test limit=0 skip=0\nTree: not_exist_field == \"\"\nSort: {}\nProj: {}\n No query solutions",
"code" : 17007
}