diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java index 4772e6ab0..826d513a9 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java @@ -209,10 +209,6 @@ public class DefaultIndexOperations implements IndexOperations { Assert.notNull(callback, "CollectionCallback must not be null"); - if (type != null) { - return mongoOperations.execute(type, callback); - } - return mongoOperations.execute(collectionName, callback); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsUnitTests.java index 3cc7153de..c83d2fc38 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsUnitTests.java @@ -43,6 +43,7 @@ import com.mongodb.client.model.IndexOptions; * Unit tests for {@link DefaultIndexOperations}. * * @author Christoph Strobl + * @author Mark Paluch */ @ExtendWith(MockitoExtension.class) public class DefaultIndexOperationsUnitTests { @@ -121,6 +122,15 @@ public class DefaultIndexOperationsUnitTests { verify(collection).createIndex(eq(new Document("firstname", "hashed")), any()); } + @Test // GH-4698 + void shouldConsiderGivenCollectionName() { + + DefaultIndexOperations operations = new DefaultIndexOperations(template, "foo", Jedi.class); + + operations.ensureIndex(HashedIndex.hashed("name")); + verify(db).getCollection(eq("foo"), any(Class.class)); + } + private DefaultIndexOperations indexOpsFor(Class type) { return new DefaultIndexOperations(template, template.getCollectionName(type), type); }