我将我的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
字段上创建复合索引?