Skip to content

Commit d7549f4

Browse files
mp911dechristophstrobl
authored andcommitted
Use collection name instead type to look up the collection for index creation.
Closes: #4698 Original Pull Request: #4699
1 parent 7821c2a commit d7549f4

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java

-4
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,6 @@ public <T> T execute(CollectionCallback<T> callback) {
209209

210210
Assert.notNull(callback, "CollectionCallback must not be null");
211211

212-
if (type != null) {
213-
return mongoOperations.execute(type, callback);
214-
}
215-
216212
return mongoOperations.execute(collectionName, callback);
217213
}
218214

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultIndexOperationsUnitTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* Unit tests for {@link DefaultIndexOperations}.
4444
*
4545
* @author Christoph Strobl
46+
* @author Mark Paluch
4647
*/
4748
@ExtendWith(MockitoExtension.class)
4849
public class DefaultIndexOperationsUnitTests {
@@ -121,6 +122,15 @@ void shouldCreateHashedIndexCorrectly() {
121122
verify(collection).createIndex(eq(new Document("firstname", "hashed")), any());
122123
}
123124

125+
@Test // GH-4698
126+
void shouldConsiderGivenCollectionName() {
127+
128+
DefaultIndexOperations operations = new DefaultIndexOperations(template, "foo", Jedi.class);
129+
130+
operations.ensureIndex(HashedIndex.hashed("name"));
131+
verify(db).getCollection(eq("foo"), any(Class.class));
132+
}
133+
124134
private DefaultIndexOperations indexOpsFor(Class<?> type) {
125135
return new DefaultIndexOperations(template, template.getCollectionName(type), type);
126136
}

0 commit comments

Comments
 (0)