Bar*_*klı 5 mongodb mongodb-query
我有以下文件结构。
{
content: 'cat dog bird',
uid: <another_unique_id>
cid: <another_unique_id>
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试搜索此集合,并希望通过uid和/或过滤结果cid。我要运行的一些查询:
1) db.mycollection.find({uid: '1', cid: '2', $text: {$search: 'cat'}});
2) db.mycollection.find({cid: '2', $text: {$search: 'cat'}});
3) db.mycollection.find({uid: '1', $text: {$search: 'cat'}});
4) db.mycollection.find({$text: {$search: 'cat'}});
//etc...
Run Code Online (Sandbox Code Playgroud)
我试图创建这样的复合索引
db.mycollection.ensureIndex({uid: 1, cid: 1, content: 'text'});
Run Code Online (Sandbox Code Playgroud)
但这仅适用于查询#1,如果我不提供其中一个字段,则会收到以下错误。
planner returned error: failed to use text index to satisfy $text query (if text index is compound, are equality predicates given for all prefix fields?)
Run Code Online (Sandbox Code Playgroud)
我尝试过的其他方法:
在uid/ cid= 上创建非化合物索引会导致大量文档被扫描
将uid cid索引移动到文本索引之后,即
db.mycollection.ensureIndex({content: 'text', uid: 1, cid: 1});
与#1 uid和cid索引相同,未使用。
有关我正在尝试的信息:http : //docs.mongodb.org/manual/tutorial/limit-number-of-items-scanned-for-text-search/
我是否缺少某些内容,或者MongoDB使用索引无法做到这一点?
不仅完整记录了预期的行为,而且我还发现您的断言是错误的。在可以满足您指定条件的标准样品上,结果将如下所示。但是首先文档参考:
- 如果复合文本索引在文本索引键之前包含键,则要执行$ text搜索,查询谓词必须在前面的键上包含相等匹配条件。
然后说明有效查询的输出:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.mycollection",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"cid" : {
"$eq" : 2
}
},
{
"uid" : {
"$eq" : 1
}
},
{
"$text" : {
"$search" : "cat",
"$language" : ""
}
}
]
},
"winningPlan" : {
"stage" : "TEXT",
"indexPrefix" : {
"uid" : 1,
"cid" : 2
},
"indexName" : "uid_1_cid_1_content_text",
"parsedTextQuery" : {
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "trashbox",
"port" : 27017,
"version" : "3.0.0",
"gitVersion" : "a841fd6394365954886924a35076691b4d149168"
},
"ok" : 1
}
Run Code Online (Sandbox Code Playgroud)
因此,如果您要发出的查询具有与实际创建的“复合键”不同的模式,并且符合明确指定的规则,那么您可能还应该注意以下要点:
- 一个集合最多可以有一个文本索引。
因此,以“任何形式”的复合形式或其他形式,如果您要查找MongoDB文本索引的多个定义,则不能这样做。这同样适用于“地理空间”索引,以及在$or表达式外部或.sort()查询引擎一次只能选择一个索引的一般考虑。
现代版本应报告非常具体的行以及错误:
(如果文本索引为复合索引,是否为所有前缀字段都提供相等谓词?)
因此,“所有”字段都是必填字段,并且它们“必须”完全匹配而不使用不等式运算符。
如果您不打算“始终”使用其他字段作为“完全匹配”条件的查询的一部分,那么您将无法与文本搜索一起形成复合索引。
| 归档时间: |
|
| 查看次数: |
4281 次 |
| 最近记录: |