public static void createOrUpdateIndexes(final MongoOperations mongoTemplate,
                                             final MongoCollection<org.bson.Document> collection,
                                             final List<? extends IndexDefinition> indexesToCreate) {
        val collectionName = collection.getNamespace().getCollectionName();
        val indexes = collection.listIndexes();
        LOGGER.debug("Existing indexes on collection [{}] are [{}]", collection.getNamespace(), indexes);
        indexesToCreate.forEach(index -> {
            var indexExistsWithDifferentOptions = false;
            val indexKeys = index.getIndexKeys();
            val indexOptions = index.getIndexOptions();
            for (val existingIndex : indexes) {
                val keyMatches = existingIndex.get("key").equals(indexKeys);
                val optionsMatch = indexOptions.entrySet().stream()
                    .allMatch(entry -> entry.getValue().equals(existingIndex.get(entry.getKey())));
                val noExtraOptions = existingIndex.keySet().stream()
                    .allMatch(key -> MONGO_INDEX_KEYS.contains(key) || indexOptions.containsKey(key));
                indexExistsWithDifferentOptions = indexExistsWithDifferentOptions || (keyMatches && !(optionsMatch && noExtraOptions));
            }

            try {
                if (indexExistsWithDifferentOptions) {
                    LOGGER.debug("Removing MongoDb index [{}] from [{}]", indexKeys, collection.getNamespace());
                    collection.dropIndex(indexKeys);
                }
                LOGGER.debug("Creating index [{}] on collection [{}]", index, collectionName);
                mongoTemplate.indexOps(collectionName).createIndex(index);
            } catch (final Exception e) {
                LoggingUtils.warn(LOGGER, e);
            }
        });
    }
Remove any index with the same indexKey but differing indexOptions in anticipation of recreating it.

@param mongoTemplate   the mongo template
@param collection      The collection to check the indexes of
@param indexesToCreate the indexes to create
support/cas-server-support-mongo-core/src/main/java/org/apereo/cas/mongo/MongoDbConnectionFactory.java