Nach einer langen Suche, ich denke, dass jetzt nicht möglich ist, Anmerkungen zu verwenden, so erstellte ich eine programmatische Möglichkeit, dieses Szenario zu verwalten.
Schaffung normaler Index und Verbundindex
Zuerst habe ich ein Objekt definiert, um die Indexstruktur abzubilden.
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;
}
Die Json Sammlung wie dies
{
"collectionName" : "example",
"normalIndexMap" : {
"sessionId" : "ASC",
"hash" : "DESC"
},
"compoundIndexMap" : [
"{\"hash\":1,\"sessionId\":1,\"serverDate\":-1}",
"{\"sessionId\":1,\"serverDate\":-1}",
"{\"sessionId\":1,\"loginWasSuccessful\":1,\"loginDate\":-1}"
]}
aussehen wird, dann können wir die Sammlung erstellen und die Indizes
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;
}
Die resultierende einzelne Indexliste kann verwendet werden, um einzelnen Feldindex zu erstellen
private void createIndexes(List<IndexDefinition> indexDefinitionList, MongoOperations mongoOperations, String collectionName) {
indexDefinitionList.forEach(indexDefinition -> mongoOperations.indexOps(collectionName).ensureIndex(indexDefinition));
}
Die Verbindung Index oder Mehrfeldindex ist schwierig, brauchen wir DBObjects erstellen Den Index kann
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;
}
Und das Ergebnis festgelegt werden, um den Index in der Sammlung
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());
}
}
ähnliche Frage http zu erstellen://stackoverflow.com/questions/23942469/create-index-in-correct-collection – Alex