在 MongoDB 中执行多键索引时跳过子文档中的某些字段
Skipping certain fields in subdocument when doing a multikey index in MongoDB
我尝试在 MongoDB(包含子文档的数组)中进行多键索引,但最终超过了许多键的字节限制。
所有子文档都包含相同的字段 - 有什么方法可以跳过较大的字段来执行多键索引吗?
类似于:
db.foo.createIndex({"bar":1}, except for baz, bundy)
只要所有元素都在一个数组中(参见 Compound indexes with MultiKey ),那么在 all 子属性上设置数组是完全合法的想要:
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,
"namespace" : "test.foo",
"indexFilterSet" : false,
"parsedQuery" : {
"bar" : {
"$elemMatch" : {
"$and" : [
{
"baz" : {
"$eq" : 1
}
},
{
"buz" : {
"$eq" : 1
}
},
{
"foo" : {
"$eq" : 1
}
}
]
}
}
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"bar" : {
"$elemMatch" : {
"$and" : [
{
"foo" : {
"$eq" : 1
}
},
{
"baz" : {
"$eq" : 1
}
},
{
"buz" : {
"$eq" : 1
}
}
]
}
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"bar.foo" : 1,
"bar.baz" : 1,
"bar.buz" : 1
},
"indexName" : "bar.foo_1_bar.baz_1_bar.buz_1",
"isMultiKey" : false,
"direction" : "forward",
"indexBounds" : {
"bar.foo" : [
"[1.0, 1.0]"
],
"bar.baz" : [
"[1.0, 1.0]"
],
"bar.buz" : [
"[1.0, 1.0]"
]
}
}
},
所以那里的 "indexName" 显示了选择的索引。
但是当然没有任何形式的索引创建允许您 "exclude" 字段。它是 "all or nothing",当然相同的字节限制适用于您可以应用的总密钥。
我尝试在 MongoDB(包含子文档的数组)中进行多键索引,但最终超过了许多键的字节限制。
所有子文档都包含相同的字段 - 有什么方法可以跳过较大的字段来执行多键索引吗?
类似于:
db.foo.createIndex({"bar":1}, except for baz, bundy)
只要所有元素都在一个数组中(参见 Compound indexes with MultiKey ),那么在 all 子属性上设置数组是完全合法的想要:
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,
"namespace" : "test.foo",
"indexFilterSet" : false,
"parsedQuery" : {
"bar" : {
"$elemMatch" : {
"$and" : [
{
"baz" : {
"$eq" : 1
}
},
{
"buz" : {
"$eq" : 1
}
},
{
"foo" : {
"$eq" : 1
}
}
]
}
}
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"bar" : {
"$elemMatch" : {
"$and" : [
{
"foo" : {
"$eq" : 1
}
},
{
"baz" : {
"$eq" : 1
}
},
{
"buz" : {
"$eq" : 1
}
}
]
}
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"bar.foo" : 1,
"bar.baz" : 1,
"bar.buz" : 1
},
"indexName" : "bar.foo_1_bar.baz_1_bar.buz_1",
"isMultiKey" : false,
"direction" : "forward",
"indexBounds" : {
"bar.foo" : [
"[1.0, 1.0]"
],
"bar.baz" : [
"[1.0, 1.0]"
],
"bar.buz" : [
"[1.0, 1.0]"
]
}
}
},
所以那里的 "indexName" 显示了选择的索引。
但是当然没有任何形式的索引创建允许您 "exclude" 字段。它是 "all or nothing",当然相同的字节限制适用于您可以应用的总密钥。