Use collection name instead type to look up the collection for index creation.

Closes: #4698
Original Pull Request: #4699
This commit is contained in:
Mark Paluch
2024-05-16 09:07:18 +02:00
committed by Christoph Strobl
parent 7821c2a545
commit d7549f4fea
2 changed files with 10 additions and 4 deletions

View File

@@ -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);
}

View File

@@ -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);
}