diff --git a/pom.xml b/pom.xml
index a48565f493..17ee99a373 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
org.springframework.data
spring-data-mongodb-parent
- 4.3.0-SNAPSHOT
+ 4.3.0-GH-4698-SNAPSHOT
pom
Spring Data MongoDB
diff --git a/spring-data-mongodb-benchmarks/pom.xml b/spring-data-mongodb-benchmarks/pom.xml
index 34d95eb205..d975d893f5 100644
--- a/spring-data-mongodb-benchmarks/pom.xml
+++ b/spring-data-mongodb-benchmarks/pom.xml
@@ -7,7 +7,7 @@
org.springframework.data
spring-data-mongodb-parent
- 4.3.0-SNAPSHOT
+ 4.3.0-GH-4698-SNAPSHOT
../pom.xml
diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml
index 124a6bf5ad..b01a7973d5 100644
--- a/spring-data-mongodb-distribution/pom.xml
+++ b/spring-data-mongodb-distribution/pom.xml
@@ -15,7 +15,7 @@
org.springframework.data
spring-data-mongodb-parent
- 4.3.0-SNAPSHOT
+ 4.3.0-GH-4698-SNAPSHOT
../pom.xml
diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml
index e7282be0fa..b681786d81 100644
--- a/spring-data-mongodb/pom.xml
+++ b/spring-data-mongodb/pom.xml
@@ -13,7 +13,7 @@
org.springframework.data
spring-data-mongodb-parent
- 4.3.0-SNAPSHOT
+ 4.3.0-GH-4698-SNAPSHOT
../pom.xml
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 4772e6ab09..33b1aea656 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
@@ -20,6 +20,7 @@
import java.util.List;
import org.bson.Document;
+
import org.springframework.dao.DataAccessException;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.UncategorizedMongoDbException;
@@ -54,7 +55,7 @@ public class DefaultIndexOperations implements IndexOperations {
private final QueryMapper mapper;
private final @Nullable Class> type;
- private MongoOperations mongoOperations;
+ private final MongoOperations mongoOperations;
/**
* Creates a new {@link DefaultIndexOperations}.
@@ -115,7 +116,7 @@ public DefaultIndexOperations(MongoOperations mongoOperations, String collection
}
@Override
- public String ensureIndex(final IndexDefinition indexDefinition) {
+ public String ensureIndex(IndexDefinition indexDefinition) {
return execute(collection -> {
@@ -149,7 +150,8 @@ private MongoPersistentEntity> lookupPersistentEntity(@Nullable Class> entit
return null;
}
- public void dropIndex(final String name) {
+ @Override
+ public void dropIndex(String name) {
execute(collection -> {
collection.dropIndex(name);
@@ -167,15 +169,18 @@ public void alterIndex(String name, org.springframework.data.mongodb.core.index.
Document result = mongoOperations
.execute(db -> db.runCommand(new Document("collMod", collectionName).append("index", indexOptions)));
- if(NumberUtils.convertNumberToTargetClass(result.get("ok", (Number) 0), Integer.class) != 1) {
- throw new UncategorizedMongoDbException("Index '%s' could not be modified. Response was %s".formatted(name, result.toJson()), null);
+ if (NumberUtils.convertNumberToTargetClass(result.get("ok", (Number) 0), Integer.class) != 1) {
+ throw new UncategorizedMongoDbException(
+ "Index '%s' could not be modified. Response was %s".formatted(name, result.toJson()), null);
}
}
+ @Override
public void dropAllIndexes() {
dropIndex("*");
}
+ @Override
public List getIndexInfo() {
return execute(new CollectionCallback>() {
@@ -209,10 +214,6 @@ public T execute(CollectionCallback callback) {
Assert.notNull(callback, "CollectionCallback must not be null");
- if (type != null) {
- return mongoOperations.execute(type, callback);
- }
-
return mongoOperations.execute(collectionName, callback);
}
@@ -228,7 +229,8 @@ private IndexOptions addPartialFilterIfPresent(IndexOptions ops, Document source
mapper.getMappedSort((Document) sourceOptions.get(PARTIAL_FILTER_EXPRESSION_KEY), entity));
}
- private static IndexOptions addDefaultCollationIfRequired(IndexOptions ops, MongoPersistentEntity> entity) {
+ private static IndexOptions addDefaultCollationIfRequired(IndexOptions ops,
+ @Nullable MongoPersistentEntity> entity) {
if (ops.getCollation() != null || entity == null || !entity.hasCollation()) {
return ops;
diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultReactiveIndexOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultReactiveIndexOperations.java
index 548d3e0862..63f4c1d9ae 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultReactiveIndexOperations.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultReactiveIndexOperations.java
@@ -116,8 +116,9 @@ public Mono alterIndex(String name, org.springframework.data.mongodb.core.
return Flux.from(db.runCommand(new Document("collMod", collectionName).append("index", indexOptions)))
.doOnNext(result -> {
- if(NumberUtils.convertNumberToTargetClass(result.get("ok", (Number) 0), Integer.class) != 1) {
- throw new UncategorizedMongoDbException("Index '%s' could not be modified. Response was %s".formatted(name, result.toJson()), null);
+ if (NumberUtils.convertNumberToTargetClass(result.get("ok", (Number) 0), Integer.class) != 1) {
+ throw new UncategorizedMongoDbException(
+ "Index '%s' could not be modified. Response was %s".formatted(name, result.toJson()), null);
}
});
}).then();
@@ -134,14 +135,17 @@ private MongoPersistentEntity> lookupPersistentEntity(String collection) {
.orElse(null);
}
- public Mono dropIndex(final String name) {
+ @Override
+ public Mono dropIndex(String name) {
return mongoOperations.execute(collectionName, collection -> collection.dropIndex(name)).then();
}
+ @Override
public Mono dropAllIndexes() {
return dropIndex("*");
}
+ @Override
public Flux getIndexInfo() {
return mongoOperations.execute(collectionName, collection -> collection.listIndexes(Document.class)) //
@@ -160,7 +164,8 @@ private IndexOptions addPartialFilterIfPresent(IndexOptions ops, Document source
queryMapper.getMappedObject((Document) sourceOptions.get(PARTIAL_FILTER_EXPRESSION_KEY), entity));
}
- private static IndexOptions addDefaultCollationIfRequired(IndexOptions ops, MongoPersistentEntity> entity) {
+ private static IndexOptions addDefaultCollationIfRequired(IndexOptions ops,
+ @Nullable MongoPersistentEntity> entity) {
if (ops.getCollation() != null || entity == null || !entity.hasCollation()) {
return ops;
diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultScriptOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultScriptOperations.java
index 127a127436..2fadefdaed 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultScriptOperations.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultScriptOperations.java
@@ -85,7 +85,7 @@ public NamedMongoScript register(NamedMongoScript script) {
}
@Override
- public Object execute(final ExecutableMongoScript script, final Object... args) {
+ public Object execute(ExecutableMongoScript script, Object... args) {
Assert.notNull(script, "Script must not be null");
@@ -104,7 +104,7 @@ public Object doInDB(MongoDatabase db) throws MongoException, DataAccessExceptio
}
@Override
- public Object call(final String scriptName, final Object... args) {
+ public Object call(String scriptName, Object... args) {
Assert.hasText(scriptName, "ScriptName must not be null or empty");
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 3cc7153dea..c83d2fc386 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 @@
* Unit tests for {@link DefaultIndexOperations}.
*
* @author Christoph Strobl
+ * @author Mark Paluch
*/
@ExtendWith(MockitoExtension.class)
public class DefaultIndexOperationsUnitTests {
@@ -121,6 +122,15 @@ void shouldCreateHashedIndexCorrectly() {
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);
}