Mongodb 索引动态命名的嵌套字段
因此,出于某种原因,开发人员决定使用动态命名的嵌套字段存储一些信息,而现在对这些字段的查询(毫不奇怪)非常缓慢 文件如下:Mongodb 索引动态命名的嵌套字段,mongodb,mongodb-indexes,Mongodb,Mongodb Indexes,因此,出于某种原因,开发人员决定使用动态命名的嵌套字段存储一些信息,而现在对这些字段的查询(毫不奇怪)非常缓慢 文件如下: { "_id:" : "2343244324", "field" : { "nested_9j3kj49j" : { // some other stuff here }, "nested_j32kj43" : { // yet some other stuff
{
"_id:" : "2343244324",
"field" : {
"nested_9j3kj49j" : {
// some other stuff here
},
"nested_j32kj43" : {
// yet some other stuff here
}
},
[other fields]
}
***
{
"_id:" : "57657656",
"field" : {
"nested_687fds6d7f6" : {
// and yet some other stuff here
}
},
[other fields]
}
所提到的慢速查询如下所示:
db.Collection.find({ "field.nested_j32kj43" : { $exists: true} });
我是否可以以更优化的方式执行此查询,或者为那些名称和存在方式不同的嵌套字段创建某种索引?索引位于值或其中的数据上,而不是键上。一种方法是添加新的索引数组字段(例如,
字段键
)将现有键作为值(例如“嵌套的\u 9j3kj49j”)添加到每个文档中,然后对该字段进行查询。索引位于值或其中的数据上,而不是位于键上。一种方法是向每个现有键作为值(例如“嵌套的\u 9j3kj49j”)的文档中添加一个新的索引数组字段(例如fieldKeys
)然后查询该字段。