diff --git a/kmongo-core/src/main/kotlin/org/litote/kmongo/MongoCollections.kt b/kmongo-core/src/main/kotlin/org/litote/kmongo/MongoCollections.kt index c2e2f61d..70f72845 100644 --- a/kmongo-core/src/main/kotlin/org/litote/kmongo/MongoCollections.kt +++ b/kmongo-core/src/main/kotlin/org/litote/kmongo/MongoCollections.kt @@ -697,7 +697,11 @@ fun MongoCollection.ensureIndex(keys: String, indexOptions: IndexOptions } catch (e: MongoCommandException) { //there is an exception if the parameters of an existing index are changed. //then drop the index and create a new one - dropIndexOfKeys(keys) + try { + dropIndexOfKeys(keys) + } catch (e2: Exception) { + //ignore + } createIndex(keys, indexOptions) } } diff --git a/kmongo-core/src/main/kotlin/org/litote/kmongo/MongoCollectionsWithClientSession.kt b/kmongo-core/src/main/kotlin/org/litote/kmongo/MongoCollectionsWithClientSession.kt index 2095e73c..8f4a8be2 100644 --- a/kmongo-core/src/main/kotlin/org/litote/kmongo/MongoCollectionsWithClientSession.kt +++ b/kmongo-core/src/main/kotlin/org/litote/kmongo/MongoCollectionsWithClientSession.kt @@ -753,7 +753,11 @@ fun MongoCollection.ensureIndex( } catch (e: MongoCommandException) { //there is an exception if the parameters of an existing index are changed. //then drop the index and create a new one - dropIndex(clientSession, keys) + try { + dropIndex(clientSession, keys) + } catch (e2: Exception) { + //ignore + } createIndex(clientSession, keys, indexOptions) } } diff --git a/kmongo-coroutine-core/src/main/kotlin/org/litote/kmongo/coroutine/MongoCollections.kt b/kmongo-coroutine-core/src/main/kotlin/org/litote/kmongo/coroutine/MongoCollections.kt index 73be7fbd..b76e3419 100644 --- a/kmongo-coroutine-core/src/main/kotlin/org/litote/kmongo/coroutine/MongoCollections.kt +++ b/kmongo-coroutine-core/src/main/kotlin/org/litote/kmongo/coroutine/MongoCollections.kt @@ -1258,7 +1258,13 @@ suspend fun MongoCollection.ensureIndex( } catch (e: MongoCommandException) { //there is an exception if the parameters of an existing index are changed. //then drop the index and create a new one - singleResult { dropIndex(keys, it) } + singleResult { + try { + dropIndex(keys, it) + } catch (e2: Exception) { + //ignore + } + } singleResult { createIndex(keys, indexOptions, it) } } diff --git a/kmongo-rxjava2-core/src/main/kotlin/org/litote/kmongo/rxjava2/MongoCollections.kt b/kmongo-rxjava2-core/src/main/kotlin/org/litote/kmongo/rxjava2/MongoCollections.kt index a5913978..e5619ccb 100644 --- a/kmongo-rxjava2-core/src/main/kotlin/org/litote/kmongo/rxjava2/MongoCollections.kt +++ b/kmongo-rxjava2-core/src/main/kotlin/org/litote/kmongo/rxjava2/MongoCollections.kt @@ -740,6 +740,7 @@ fun MongoCollection.ensureIndex(keys: String, indexOptions: IndexOptions return createIndex(keys, indexOptions) .onErrorResumeNext( dropIndex(keys) + .onErrorComplete() .andThen(createIndex(keys, indexOptions)) ) .flatMapCompletable { _ -> @@ -763,6 +764,7 @@ fun MongoCollection.ensureIndex( return maybeResult { createIndex(keys, indexOptions, it) } .onErrorResumeNext( completableResult { dropIndex(keys, it) } + .onErrorComplete() .andThen(maybeResult { createIndex(keys, indexOptions, it) }) ) .flatMapCompletable { _ -> diff --git a/kmongo-rxjava2-core/src/main/kotlin/org/litote/kmongo/rxjava2/ReactiveStreamsMongoCollections.kt b/kmongo-rxjava2-core/src/main/kotlin/org/litote/kmongo/rxjava2/ReactiveStreamsMongoCollections.kt index af79ae4b..9bd5c21f 100644 --- a/kmongo-rxjava2-core/src/main/kotlin/org/litote/kmongo/rxjava2/ReactiveStreamsMongoCollections.kt +++ b/kmongo-rxjava2-core/src/main/kotlin/org/litote/kmongo/rxjava2/ReactiveStreamsMongoCollections.kt @@ -547,6 +547,7 @@ fun MongoCollection.ensureIndex(keys: String, indexOptions: IndexOptions .onErrorResumeNext( dropIndex(keys) .completable() + .onErrorComplete() .andThen(createIndex(keys, indexOptions)) ) .flatMapCompletable { _ -> @@ -569,7 +570,9 @@ fun MongoCollection.ensureIndex( ): Completable { return createIndex(keys, indexOptions).maybe() .onErrorResumeNext( - dropIndex(keys).completable() + dropIndex(keys) + .completable() + .onErrorComplete() .andThen(createIndex(keys, indexOptions).maybe()) ) .flatMapCompletable { _ ->