我尝试在MongoDB(包含子文档的数组)中执行多键索引,但最终超过了许多键的字节限制。
所有子文档都包含相同的字段 - 有没有办法做一个多键索引,跳过更大的字段?
类似的东西:
db.foo.createIndex({"bar":1}, except for baz, bundy)
1 个答案:
答案 0 :(得分:0)
只要所有元素都在一个数组中(请参阅使用MultiKey的复合索引),那么在所需的所有子属性上设置数组是完全合法的:
db.foo.insert({" bar":[{" foo":1," baz":1," buz":1 ," bat":1}]})
db.foo.ensureIndex({" bar.foo":1," bar.baz":1," bar.buz":1})
查询通常会使用索引:
db.foo.find({" bar":{" $ elemMatch":{" foo":1," bar":1, " buz":1}}})。explain()
{
" queryPlanner" :{
" plannerVersion" :1,
"命名空间" :" test.foo",
" indexFilterSet" :假的,
" parsedQuery" :{
"杆" :{
" $ elemMatch" :{
" $和" :[
{
"巴兹" :{
" $当量" :1
}
},
{
" BUZ" :{
" $当量" :1
}
},
{
"富" :{
" $当量" :1
}
}
]
}
}
},
" winningPlan" :{
"级" :" FETCH",
"过滤器" :{
"杆" :{
" $ elemMatch" :{
" $和" :[
{
"富" :{
" $当量" :1
}
},
{
"巴兹" :{
" $当量" :1
}
},
{
" BUZ" :{
" $当量" :1
}
}
]
}
}
},
" inputStage" :{
"级" :" IXSCAN",
" keyPattern" :{
" bar.foo" :1,
" bar.baz" :1,
" bar.buz" :1
},
" INDEXNAME" :" bar.foo_1_bar.baz_1_bar.buz_1",
" isMultiKey" :假的,
"方向" :"转发",
" indexBounds" :{
" bar.foo" :[
" [1.0,1.0]"
]
" bar.baz" :[
" [1.0,1.0]"
]
" bar.buz" :[
" [1.0,1.0]"
]
}
}
},
所以" indexName"显示选择的索引。
但是,当然没有任何形式的索引创建允许你排除"领域。它全部或全部"当然,相同的字节限制适用于您可以应用的总密钥。