经过长时间的搜索,我认为目前无法使用注释,因此我创建了一种以编程方式管理此场景的方法。
创建普通索引和复合索引
首先我定义了一个对象来映射索引结构。
public class CollectionDefinition {
private String id;
private String collectionName;
private LinkedHashMap<String, Sort.Direction> normalIndexMap;
private ArrayList<String> compoundIndexMap;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCollectionName() {
return collectionName;
}
public void setCollectionName(String collectionName) {
this.collectionName = collectionName;
}
public LinkedHashMap<String, Sort.Direction> getNormalIndexMap() {
return normalIndexMap;
}
public void setNormalIndexMap(LinkedHashMap<String, Sort.Direction> normalIndexMap) {
this.normalIndexMap = normalIndexMap;
}
public ArrayList<String> getCompoundIndexMap() {
return compoundIndexMap;
}
public void setCompoundIndexMap(ArrayList<String> compoundIndexMap) {
this.compoundIndexMap = compoundIndexMap;
}
Json 集合看起来像这样
{
"collectionName" : "example",
"normalIndexMap" : {
"sessionId" : "ASC",
"hash" : "DESC"
},
"compoundIndexMap" : [
"{\"hash\":1,\"sessionId\":1,\"serverDate\":-1}",
"{\"sessionId\":1,\"serverDate\":-1}",
"{\"sessionId\":1,\"loginWasSuccessful\":1,\"loginDate\":-1}"
]}
然后我们可以创建集合和索引
private List<IndexDefinition> createIndexList(Map<String, Sort.Direction> normalIndexMap) {
Set<String> keySet = normalIndexMap.keySet();
List<IndexDefinition> indexArray = new ArrayList<>();
for (String key : keySet) {
indexArray.add(new Index(key, normalIndexMap.get(key)).background());
}
return indexArray;
}
生成的单索引列表可用于创建单字段索引
private void createIndexes(List<IndexDefinition> indexDefinitionList, MongoOperations mongoOperations, String collectionName) {
indexDefinitionList.forEach(indexDefinition -> mongoOperations.indexOps(collectionName).ensureIndex(indexDefinition));
}
复合索引或者多字段索引比较棘手,我们需要创建DBObjects来定义索引
private List<DBObject> createCompountIndexList(List<String> compoundIndexStringMapList) {//NOSONAR IS IN USE
if (compoundIndexStringMapList == null || compoundIndexStringMapList.isEmpty())
return new ArrayList<>(); //NOSONAR
ObjectMapper mapper = new ObjectMapper();
List<DBObject> basicDBObjectList = new ArrayList<>();
for (String stringMapList : compoundIndexStringMapList) {
LinkedHashMap<String, Integer> parsedMap;
try {
parsedMap = mapper.readValue(stringMapList, new TypeReference<Map<String, Integer>>() {
});
BasicDBObjectBuilder dbObjectBuilder = BasicDBObjectBuilder.start();
Iterator it = parsedMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry indexElement = (Map.Entry) it.next();
dbObjectBuilder.add((String) indexElement.getKey(), indexElement.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
basicDBObjectList.add(dbObjectBuilder.get());
} catch (IOException e) {//NOSONAR I´m logging it, we can not do anything more here cause it is part of the db configuration settings that need to be correct
Logger.getLogger(JsonCollectionCreation.class).error("The compound index definition " + stringMapList + " is not correct it can not be mapped to LinkHashMap");
}
}
return basicDBObjectList;
}
并且结果可用于在集合中创建索引
private void createCompoundIndex(List<DBObject> compoundIndexList, MongoOperations mongoOperations, String collectionName) {
if (compoundIndexList.isEmpty()) return;
for (DBObject compoundIndexDefinition : compoundIndexList) {
mongoOperations.indexOps(collectionName).ensureIndex(new CompoundIndexDefinition(compoundIndexDefinition).background());
}
}