避免在Spring MongoDB Document的字段上创建复合索引

时间:2020-03-26 12:39:28

标签: spring mongodb spring-boot spring-data mongodb-indexes

我将我的MongoDB集合配置为带有@Document批注的域类。这是两个这样的@Document

@Document
@CompoundIndex(def = "{'name': 1, 'group': 1}", name = "source_name_group_composite", unique = true)
class Source {
    String name;
    String group;
}

@Document
class Problem {
    String title;
    Source source;
}

我已将Spring配置为在MongoDB上自动创建索引。我希望上面代码块第二行的CompoundIndex会自动创建,并且确实可以做到:

> db.source.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "mobtools.source"
        },
        {
                "v" : 2,
                "unique" : true,
                "key" : {
                        "name" : 1,
                        "group" : 1
                },
                "name" : "source_name_group_composite",
                "ns" : "mobtools.source"
        }
]

但是,这也会在Problem.source字段上创建相同的复合索引:

> db.problem.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "mobtools.problem"
        },
        {
                "v" : 2,
                "unique" : true,
                "key" : {
                        "source.name" : 1,
                        "source.group" : 1
                },
                "name" : "source.source_name_group_composite",
                "ns" : "mobtools.problem"
        }
]

我不希望创建索引source.source_name_group_composite。有没有办法告诉Spring在source集合上创建复合索引,而不是在source集合的problem字段上创建复合索引?

0 个答案:

没有答案
相关问题