以下问题参考我的例子。
如何在idsAndNames地图中为ids创建索引?
我想搜索这些数字:7065362,7064458,7065003,7064286 ...
如何在subTargets列表中为字符串创建索引?
我想搜索这些字符串:ALL,ACTION,DRAMA ......
我的例子:
{
"_id" : ObjectId("sdfsdfsdfsdfsdfsd910d41ad9"),
"targets" : {
"Superheroes" : {
"idsAndNames" : {
"7065362" : "Doctor Strange",
"7064458" : "Dr. Manhattan",
"7065003" : "Captain Atom",
"7064286" : "Hulk"
},
"subTargets" : [
"ALL",
"ACTION",
"DRAMA"
]
}
"BestSuperheroes" : {
"idsAndNames" : {
"7065362" : "Superman",
"7064458" : "Thor",
"7065003" : "Professor X",
"7064286" : "Batman"
},
"subTargets" : [
"ALL",
"SCIFI",
"CHICKFLICKS"
]
}
}
...
}
答案 0 :(得分:0)
在尝试创建索引之前,您应该检查您的架构,将其更改为:
...
"Superheores" : [
{
"id": "7065362",
"name": "Doctor Strange"
}]
所以你可以使用db.collection.ensureIndex({'targets.Superheroes.id': 1});
。如果您有动态密钥名称,则无法创建有效索引。
对于文字,您可以尝试the text search index。