我的文件结构如下:
{
"_id": ...,
"name": "Document name",
"properties": {
"prop1": "something",
"2ndprop": "other_prop",
"other3": ["tag1", "tag2"],
}
}我无法知道properties子文档中的实际字段名(它们是由应用程序用户提供的),所以无法创建像properties.prop1这样的索引。我也不知道字段值的结构,它们可以是单个值、嵌入式文档或数组。
有什么实用的方法可以用这种模式设计对集合进行性能查询吗?
我想到的一个选项是向文档中添加一个新字段,对其进行索引,并在此字段中设置每个文档的使用字段名。
{
"_id": ...,
"name": "Document name",
"properties": {
"prop1": "something",
"2ndprop": "other_prop",
"other3": ["tag1", "tag2"],
},
"property_fields": ["prop1", "2ndprop", "other3"]
}现在,我可以首先对property_fields字段运行查询,然后让MongoDB扫描找到的文档,看看properties.prop1是否包含所需的值。这肯定是比较慢,但可能是可行的。
发布于 2013-11-26 14:00:31
处理此问题的一种方法是使用模式,如下所示。
{
"name" : "Document name",
"properties" : [
{
"k" : "prop1",
"v" : "something"
},
{
"k" : "2ndprop",
"v" : "other_prop"
},
{
"k" : "other3",
"v" : "tag1"
},
{
"k" : "other3",
"v" : "tag2"
}
]
}然后可以索引“properties.k ties.k”和“properties.k ties.v”,例如:
db.foo.ensureIndex({"properties.k": 1, "properties.v": 1})https://stackoverflow.com/questions/20218003
复制相似问题