diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java
index a71e4f5a8..69c1745f8 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java
@@ -55,8 +55,7 @@ import com.mongodb.client.result.UpdateResult;
/**
* Interface that specifies a basic set of MongoDB operations. Implemented by {@link MongoTemplate}. Not often used but
* a useful option for extensibility and testability (as it can be easily mocked, stubbed, or be the target of a JDK
- * proxy).
- *
+ * proxy).
* NOTE: Some operations cannot be executed within a MongoDB transaction. Please refer to the MongoDB
* specific documentation to learn more about Multi
* Document Transactions.
@@ -82,7 +81,7 @@ public interface MongoOperations extends FluentMongoOperations {
String getCollectionName(Class> entityClass);
/**
- * Execute the a MongoDB command expressed as a JSON string. Parsing is delegated to {@link Document#parse(String)} to
+ * Execute a MongoDB command expressed as a JSON string. Parsing is delegated to {@link Document#parse(String)} to
* obtain the {@link Document} holding the actual command. Any errors that result from executing this command will be
* converted into Spring's DAO exception hierarchy.
*
@@ -122,8 +121,7 @@ public interface MongoOperations extends FluentMongoOperations {
void executeQuery(Query query, String collectionName, DocumentCallbackHandler dch);
/**
- * Executes a {@link DbCallback} translating any exceptions as necessary.
- *
+ * Executes a {@link DbCallback} translating any exceptions as necessary.
* Allows for returning a result object, that is a domain object or a collection of domain objects.
*
* @param action callback object that specifies the MongoDB actions to perform on the passed in DB instance. Must not
@@ -135,8 +133,7 @@ public interface MongoOperations extends FluentMongoOperations {
T execute(DbCallback action);
/**
- * Executes the given {@link CollectionCallback} on the entity collection of the specified class.
- *
+ * Executes the given {@link CollectionCallback} on the entity collection of the specified class.
* Allows for returning a result object, that is a domain object or a collection of domain objects.
*
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
@@ -148,8 +145,7 @@ public interface MongoOperations extends FluentMongoOperations {
T execute(Class> entityClass, CollectionCallback action);
/**
- * Executes the given {@link CollectionCallback} on the collection of the given name.
- *
+ * Executes the given {@link CollectionCallback} on the collection of the given name.
* Allows for returning a result object, that is a domain object or a collection of domain objects.
*
* @param collectionName the name of the collection that specifies which {@link MongoCollection} instance will be
@@ -173,8 +169,7 @@ public interface MongoOperations extends FluentMongoOperations {
/**
* Obtain a {@link ClientSession session} bound instance of {@link SessionScoped} binding the {@link ClientSession}
- * provided by the given {@link Supplier} to each and every command issued against MongoDB.
- *
+ * provided by the given {@link Supplier} to each and every command issued against MongoDB.
* Note: It is up to the caller to manage the {@link ClientSession} lifecycle. Use the
* {@link SessionScoped#execute(SessionCallback, Consumer)} hook to potentially close the {@link ClientSession}.
*
@@ -209,8 +204,7 @@ public interface MongoOperations extends FluentMongoOperations {
}
/**
- * Obtain a {@link ClientSession} bound instance of {@link MongoOperations}.
- *
+ * Obtain a {@link ClientSession} bound instance of {@link MongoOperations}.
* Note: It is up to the caller to manage the {@link ClientSession} lifecycle.
*
* @param session must not be {@literal null}.
@@ -297,8 +291,7 @@ public interface MongoOperations extends FluentMongoOperations {
* Get a {@link MongoCollection} by its name. The returned collection may not exists yet (except in local memory) and
* is created on first interaction with the server. Collections can be explicitly created via
* {@link #createCollection(Class)}. Please make sure to check if the collection {@link #collectionExists(Class)
- * exists} first.
- *
+ * exists} first.
* Translate any exceptions as necessary.
*
* @param collectionName name of the collection. Must not be {@literal null}.
@@ -307,8 +300,7 @@ public interface MongoOperations extends FluentMongoOperations {
MongoCollection getCollection(String collectionName);
/**
- * Check to see if a collection with a name indicated by the entity class exists.
- *
+ * Check to see if a collection with a name indicated by the entity class exists.
* Translate any exceptions as necessary.
*
* @param entityClass class that determines the name of the collection. Must not be {@literal null}.
@@ -317,8 +309,7 @@ public interface MongoOperations extends FluentMongoOperations {
boolean collectionExists(Class entityClass);
/**
- * Check to see if a collection with a given name exists.
- *
+ * Check to see if a collection with a given name exists.
* Translate any exceptions as necessary.
*
* @param collectionName name of the collection. Must not be {@literal null}.
@@ -327,8 +318,7 @@ public interface MongoOperations extends FluentMongoOperations {
boolean collectionExists(String collectionName);
/**
- * Drop the collection with the name indicated by the entity class.
- *
+ * Drop the collection with the name indicated by the entity class.
* Translate any exceptions as necessary.
*
* @param entityClass class that determines the collection to drop/delete. Must not be {@literal null}.
@@ -336,8 +326,7 @@ public interface MongoOperations extends FluentMongoOperations {
void dropCollection(Class entityClass);
/**
- * Drop the collection with the given name.
- *
+ * Drop the collection with the given name.
* Translate any exceptions as necessary.
*
* @param collectionName name of the collection to drop/delete.
@@ -400,11 +389,9 @@ public interface MongoOperations extends FluentMongoOperations {
BulkOperations bulkOps(BulkMode mode, @Nullable Class> entityType, String collectionName);
/**
- * Query for a list of objects of type T from the collection used by the entity class.
- *
+ * Query for a list of objects of type T from the collection used by the entity class.
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless
- * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
- *
+ * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
* If your collection does not contain a homogeneous collection of types, this operation will not be an efficient way
* to map objects since the test for class type is done in the client and not on the server.
*
@@ -414,11 +401,9 @@ public interface MongoOperations extends FluentMongoOperations {
List findAll(Class entityClass);
/**
- * Query for a list of objects of type T from the specified collection.
- *
+ * Query for a list of objects of type T from the specified collection.
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless
- * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
- *
+ * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
* If your collection does not contain a homogeneous collection of types, this operation will not be an efficient way
* to map objects since the test for class type is done in the client and not on the server.
*
@@ -674,11 +659,9 @@ public interface MongoOperations extends FluentMongoOperations {
/**
* Map the results of an ad-hoc query on the collection for the entity class to a single instance of an object of the
- * specified type.
- *
+ * specified type.
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless
- * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
- *
+ * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link Query}.
*
@@ -692,11 +675,9 @@ public interface MongoOperations extends FluentMongoOperations {
/**
* Map the results of an ad-hoc query on the specified collection to a single instance of an object of the specified
- * type.
- *
+ * type.
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless
- * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
- *
+ * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link Query}.
*
@@ -740,11 +721,9 @@ public interface MongoOperations extends FluentMongoOperations {
boolean exists(Query query, @Nullable Class> entityClass, String collectionName);
/**
- * Map the results of an ad-hoc query on the collection for the entity class to a List of the specified type.
- *
+ * Map the results of an ad-hoc query on the collection for the entity class to a List of the specified type.
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless
- * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
- *
+ * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link Query}.
*
@@ -756,11 +735,9 @@ public interface MongoOperations extends FluentMongoOperations {
List find(Query query, Class entityClass);
/**
- * Map the results of an ad-hoc query on the specified collection to a List of the specified type.
- *
+ * Map the results of an ad-hoc query on the specified collection to a List of the specified type.
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless
- * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
- *
+ * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link Query}.
*
@@ -1092,10 +1069,8 @@ public interface MongoOperations extends FluentMongoOperations {
/**
* Map the results of an ad-hoc query on the collection for the entity type to a single instance of an object of the
* specified type. The first document that matches the query is returned and also removed from the collection in the
- * database.
- *
- * The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}.
- *
+ * database.
+ * The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}.
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link Query}.
*
@@ -1112,8 +1087,7 @@ public interface MongoOperations extends FluentMongoOperations {
* type. The first document that matches the query is returned and also removed from the collection in the database.
*
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless
- * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
- *
+ * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
* feature rich {@link Query}.
*
@@ -1132,8 +1106,96 @@ public interface MongoOperations extends FluentMongoOperations {
* NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
* influence on the resulting number of documents found as those values are passed on to the server and potentially
* limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
- * count all matches.
+ * count all matches.
+ * This method may choose to use {@link #estimatedCount(Class)} for empty queries instead of running an
+ * {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
+ * aggregation execution} which may have an impact on performance.
+ *
+ * @param query the {@link Query} class that specifies the criteria used to find documents. Must not be
+ * {@literal null}.
+ * @param entityClass class that determines the collection to use. Must not be {@literal null}.
+ * @return the count of matching documents.
+ * @see #exactCount(Query, Class)
+ * @see #estimatedCount(Class)
+ */
+ long count(Query query, Class> entityClass);
+
+ /**
+ * Returns the number of documents for the given {@link Query} querying the given collection. The given {@link Query}
+ * must solely consist of document field references as we lack type information to map potential property references
+ * onto document fields. Use {@link #count(Query, Class, String)} to get full type specific support.
+ * NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
+ * influence on the resulting number of documents found as those values are passed on to the server and potentially
+ * limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
+ * count all matches.
+ * This method may choose to use {@link #estimatedCount(Class)} for empty queries instead of running an
+ * {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
+ * aggregation execution} which may have an impact on performance.
+ *
+ * @param query the {@link Query} class that specifies the criteria used to find documents.
+ * @param collectionName must not be {@literal null} or empty.
+ * @return the count of matching documents.
+ * @see #count(Query, Class, String)
+ * @see #exactCount(Query, String)
+ * @see #estimatedCount(String)
+ */
+ long count(Query query, String collectionName);
+
+ /**
+ * Returns the number of documents for the given {@link Query} by querying the given collection using the given entity
+ * class to map the given {@link Query}.
+ * NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
+ * influence on the resulting number of documents found as those values are passed on to the server and potentially
+ * limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
+ * count all matches.
+ * This method may choose to use {@link #estimatedCount(Class)} for empty queries instead of running an
+ * {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
+ * aggregation execution} which may have an impact on performance.
+ *
+ * @param query the {@link Query} class that specifies the criteria used to find documents. Must not be
+ * {@literal null}.
+ * @param entityClass the parametrized type. Can be {@literal null}.
+ * @param collectionName must not be {@literal null} or empty.
+ * @return the count of matching documents.
+ * @see #count(Query, Class, String)
+ * @see #estimatedCount(String)
+ */
+ long count(Query query, @Nullable Class> entityClass, String collectionName);
+
+ /**
+ * Estimate the number of documents, in the collection {@link #getCollectionName(Class) identified by the given type},
+ * based on collection statistics.
+ * Please make sure to read the MongoDB reference documentation about limitations on eg. sharded cluster or inside
+ * transactions.
+ *
+ * @param entityClass must not be {@literal null}.
+ * @return the estimated number of documents.
+ * @since 3.1
+ */
+ default long estimatedCount(Class> entityClass) {
+
+ Assert.notNull(entityClass, "Entity class must not be null!");
+ return estimatedCount(getCollectionName(entityClass));
+ }
+
+ /**
+ * Estimate the number of documents in the given collection based on collection statistics.
+ * Please make sure to read the MongoDB reference documentation about limitations on eg. sharded cluster or inside
+ * transactions.
+ *
+ * @param collectionName must not be {@literal null}.
+ * @return the estimated number of documents.
+ * @since 3.1
+ */
+ long estimatedCount(String collectionName);
+
+ /**
+ * Returns the number of documents for the given {@link Query} by querying the collection of the given entity class.
*
+ * NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
+ * influence on the resulting number of documents found as those values are passed on to the server and potentially
+ * limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
+ * count all matches.
* This method uses an
* {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
* aggregation execution} even for empty {@link Query queries} which may have an impact on performance, but guarantees
@@ -1157,8 +1219,7 @@ public interface MongoOperations extends FluentMongoOperations {
* NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
* influence on the resulting number of documents found as those values are passed on to the server and potentially
* limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
- * count all matches.
- *
+ * count all matches.
* This method uses an
* {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
* aggregation execution} even for empty {@link Query queries} which may have an impact on performance, but guarantees
@@ -1181,8 +1242,7 @@ public interface MongoOperations extends FluentMongoOperations {
* NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
* influence on the resulting number of documents found as those values are passed on to the server and potentially
* limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
- * count all matches.
- *
+ * count all matches.
* This method uses an
* {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
* aggregation execution} even for empty {@link Query queries} which may have an impact on performance, but guarantees
@@ -1199,104 +1259,13 @@ public interface MongoOperations extends FluentMongoOperations {
long exactCount(Query query, @Nullable Class> entityClass, String collectionName);
/**
- * Returns the number of documents for the given {@link Query} by querying the collection of the given entity class.
- *
- * NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
- * influence on the resulting number of documents found as those values are passed on to the server and potentially
- * limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
- * count all matches.
- *
- * This method may choose to use {@link #estimatedCount(Class)} for empty queries instead of running an
- * {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
- * aggregation execution} which may have an impact on performance.
- *
- * @param query the {@link Query} class that specifies the criteria used to find documents. Must not be
- * {@literal null}.
- * @param entityClass class that determines the collection to use. Must not be {@literal null}.
- * @return the count of matching documents.
- */
- long count(Query query, Class> entityClass);
-
- /**
- * Returns the number of documents for the given {@link Query} querying the given collection. The given {@link Query}
- * must solely consist of document field references as we lack type information to map potential property references
- * onto document fields. Use {@link #count(Query, Class, String)} to get full type specific support.
- * NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
- * influence on the resulting number of documents found as those values are passed on to the server and potentially
- * limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
- * count all matches.
- *
- * This method may choose to use {@link #estimatedCount(Class)} for empty queries instead of running an
- * {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
- * aggregation execution} which may have an impact on performance.
- *
- * @param query the {@link Query} class that specifies the criteria used to find documents.
- * @param collectionName must not be {@literal null} or empty.
- * @return the count of matching documents.
- * @see #count(Query, Class, String)
- */
- long count(Query query, String collectionName);
-
- /**
- * Estimate the number of documents, in the collection {@link #getCollectionName(Class) identified by the given type},
- * based on collection statistics.
- *
- * Please make sure to read the MongoDB reference documentation about limitations on eg. sharded cluster or inside
- * transactions.
- *
- * @param entityClass must not be {@literal null}.
- * @return the estimated number of documents.
- * @since 3.1
- */
- default long estimatedCount(Class> entityClass) {
-
- Assert.notNull(entityClass, "Entity class must not be null!");
- return estimatedCount(getCollectionName(entityClass));
- }
-
- /**
- * Estimate the number of documents in the given collection based on collection statistics.
- *
- * Please make sure to read the MongoDB reference documentation about limitations on eg. sharded cluster or inside
- * transactions.
- *
- * @param collectionName must not be {@literal null}.
- * @return the estimated number of documents.
- * @since 3.1
- */
- long estimatedCount(String collectionName);
-
- /**
- * Returns the number of documents for the given {@link Query} by querying the given collection using the given entity
- * class to map the given {@link Query}.
- * NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
- * influence on the resulting number of documents found as those values are passed on to the server and potentially
- * limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
- * count all matches.
- *
- * This method may choose to use {@link #estimatedCount(Class)} for empty queries instead of running an
- * {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
- * aggregation execution} which may have an impact on performance.
- *
- * @param query the {@link Query} class that specifies the criteria used to find documents. Must not be
- * {@literal null}.
- * @param entityClass the parametrized type. Can be {@literal null}.
- * @param collectionName must not be {@literal null} or empty.
- * @return the count of matching documents.
- */
- long count(Query query, @Nullable Class> entityClass, String collectionName);
-
- /**
- * Insert the object into the collection for the entity type of the object to save.
- *
- * The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}.
- *
+ * Insert the object into the collection for the entity type of the object to save.
+ * The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}.
* If your object has an "Id' property, it will be set with the generated Id from MongoDB. If your Id property is a
* String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your
* property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
* Spring's
- * Type Conversion" for more details.
- *
+ * Type Conversion" for more details.
* Insert is used to initially store the object into the database. To update an existing object use the save method.
*
* The {@code objectToSave} must not be collection-like.
@@ -1308,11 +1277,9 @@ public interface MongoOperations extends FluentMongoOperations {
T insert(T objectToSave);
/**
- * Insert the object into the specified collection.
- *
+ * Insert the object into the specified collection.
* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. Unless
- * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
- *
+ * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
* Insert is used to initially store the object into the database. To update an existing object use the save method.
*
* The {@code objectToSave} must not be collection-like.
@@ -1353,17 +1320,14 @@ public interface MongoOperations extends FluentMongoOperations {
/**
* Save the object to the collection for the entity type of the object to save. This will perform an insert if the
- * object is not already present, that is an 'upsert'.
- *
+ * object is not already present, that is an 'upsert'.
* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. Unless
- * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
- *
+ * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
* If your object has an "Id' property, it will be set with the generated Id from MongoDB. If your Id property is a
* String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your
* property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
* Spring's
- * Type Conversion" for more details.
- *
+ * Type Conversion" for more details.
* The {@code objectToSave} must not be collection-like.
*
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
@@ -1374,16 +1338,14 @@ public interface MongoOperations extends FluentMongoOperations {
/**
* Save the object to the specified collection. This will perform an insert if the object is not already present, that
- * is an 'upsert'.
- *
+ * is an 'upsert'.
* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. Unless
- * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
- *
+ * configured otherwise, an instance of {@link MappingMongoConverter} will be used.
* If your object has an "Id' property, it will be set with the generated Id from MongoDB. If your Id property is a
* String then MongoDB ObjectId will be used to populate that string. Otherwise, the conversion from ObjectId to your
- * property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API.
- * See Spring's Type Conversion for more details.
- *
+ * property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
+ * Spring's Type
+ * Conversion for more details.
* The {@code objectToSave} must not be collection-like.
*
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
index 03b603959..078b5eeac 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
@@ -342,10 +342,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
}
/**
- * En-/Disable usage of estimated count.
+ * Configure whether to use estimated count. Defaults to exact counting.
*
- * @param enabled if {@literal true} {@link MongoCollection#estimatedDocumentCount()} ()} will we used for unpaged,
- * empty {@link Query queries}.
+ * @param enabled use {@link com.mongodb.client.MongoCollection#estimatedDocumentCount()} for unpaged and empty
+ * {@link Query queries} if {@code true}.
* @since 3.4
*/
public void useEstimatedCount(boolean enabled) {
@@ -353,10 +353,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
}
/**
- * En-/Disable usage of estimated count based on the given {@link BiPredicate estimationFilter}.
+ * Configure whether to use estimated count based on the given {@link BiPredicate estimationFilter}.
*
- * @param enabled if {@literal true} {@link MongoCollection#estimatedDocumentCount()} will we used for {@link Document
- * filter queries} that pass the given {@link BiPredicate estimationFilter}.
+ * @param enabled use {@link com.mongodb.client.MongoCollection#estimatedDocumentCount()} for unpaged and empty
+ * {@link Query queries} if {@code true}.
* @param estimationFilter the {@link BiPredicate filter}.
* @since 3.4
*/
@@ -1013,17 +1013,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
return count(query, null, collectionName);
}
- @Override
- public long exactCount(Query query, @Nullable Class> entityClass, String collectionName) {
-
- CountContext countContext = queryOperations.countQueryContext(query);
-
- CountOptions options = countContext.getCountOptions(entityClass);
- Document mappedQuery = countContext.getMappedQuery(entityClass, mappingContext::getPersistentEntity);
-
- return doExactCount(collectionName, mappedQuery, options);
- }
-
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.MongoOperations#count(org.springframework.data.mongodb.core.query.Query, java.lang.Class, java.lang.String)
@@ -1041,7 +1030,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
return doCount(collectionName, mappedQuery, options);
}
- @SuppressWarnings("ConstantConditions")
protected long doCount(String collectionName, Document filter, CountOptions options) {
if (LOGGER.isDebugEnabled()) {
@@ -1052,6 +1040,30 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
return countExecution.countDocuments(collectionName, filter, options);
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.mongodb.core.MongoOperations#estimatedCount(java.lang.String)
+ */
+ @Override
+ public long estimatedCount(String collectionName) {
+ return doEstimatedCount(collectionName, new EstimatedDocumentCountOptions());
+ }
+
+ protected long doEstimatedCount(String collectionName, EstimatedDocumentCountOptions options) {
+ return execute(collectionName, collection -> collection.estimatedDocumentCount(options));
+ }
+
+ @Override
+ public long exactCount(Query query, @Nullable Class> entityClass, String collectionName) {
+
+ CountContext countContext = queryOperations.countQueryContext(query);
+
+ CountOptions options = countContext.getCountOptions(entityClass);
+ Document mappedQuery = countContext.getMappedQuery(entityClass, mappingContext::getPersistentEntity);
+
+ return doExactCount(collectionName, mappedQuery, options);
+ }
+
protected long doExactCount(String collectionName, Document filter, CountOptions options) {
return execute(collectionName,
collection -> collection.countDocuments(CountQuery.of(filter).toQueryDocument(), options));
@@ -1072,19 +1084,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
return options.getLimit() <= 0 && options.getSkip() <= 0;
}
- /*
- * (non-Javadoc)
- * @see org.springframework.data.mongodb.core.MongoOperations#estimatedCount(java.lang.String)
- */
- @Override
- public long estimatedCount(String collectionName) {
- return doEstimatedCount(collectionName, new EstimatedDocumentCountOptions());
- }
-
- protected long doEstimatedCount(String collectionName, EstimatedDocumentCountOptions options) {
- return execute(collectionName, collection -> collection.estimatedDocumentCount(options));
- }
-
@Override
public T insert(T objectToSave) {
diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoOperations.java
index d19775a90..d35727b23 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoOperations.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoOperations.java
@@ -89,7 +89,7 @@ public interface ReactiveMongoOperations extends ReactiveFluentMongoOperations {
ReactiveIndexOperations indexOps(Class> entityClass);
/**
- * Execute the a MongoDB command expressed as a JSON string. This will call the method JSON.parse that is part of the
+ * Execute a MongoDB command expressed as a JSON string. This will call the method JSON.parse that is part of the
* MongoDB driver to convert the JSON string to a Document. Any errors that result from executing this command will be
* converted into Spring's DAO exception hierarchy.
*
@@ -200,7 +200,6 @@ public interface ReactiveMongoOperations extends ReactiveFluentMongoOperations {
* Obtain a {@link ClientSession} bound instance of {@link ReactiveMongoOperations}.
* Note: It is up to the caller to manage the {@link ClientSession} lifecycle.
*
- * @param session must not be {@literal null}.
* @return {@link ClientSession} bound instance of {@link ReactiveMongoOperations}.
* @since 2.1
*/
@@ -868,6 +867,95 @@ public interface ReactiveMongoOperations extends ReactiveFluentMongoOperations {
*/
Mono findAndRemove(Query query, Class entityClass, String collectionName);
+ /**
+ * Returns the number of documents for the given {@link Query} by querying the collection of the given entity class.
+ *
+ * NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
+ * influence on the resulting number of documents found as those values are passed on to the server and potentially
+ * limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
+ * count all matches.
+ * This method may choose to use {@link #estimatedCount(Class)} for empty queries instead of running an
+ * {@link com.mongodb.reactivestreams.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
+ * aggregation execution} which may have an impact on performance.
+ *
+ * @param query the {@link Query} class that specifies the criteria used to find documents. Must not be
+ * {@literal null}.
+ * @param entityClass class that determines the collection to use. Must not be {@literal null}.
+ * @return the count of matching documents.
+ * @see #exactCount(Query, Class)
+ * @see #estimatedCount(Class)
+ */
+ Mono count(Query query, Class> entityClass);
+
+ /**
+ * Returns the number of documents for the given {@link Query} querying the given collection. The given {@link Query}
+ * must solely consist of document field references as we lack type information to map potential property references
+ * onto document fields. Use {@link #count(Query, Class, String)} to get full type specific support.
+ * NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
+ * influence on the resulting number of documents found as those values are passed on to the server and potentially
+ * limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
+ * count all matches.
+ * This method may choose to use {@link #estimatedCount(Class)} for empty queries instead of running an
+ * {@link com.mongodb.reactivestreams.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
+ * aggregation execution} which may have an impact on performance.
+ *
+ * @param query the {@link Query} class that specifies the criteria used to find documents.
+ * @param collectionName must not be {@literal null} or empty.
+ * @return the count of matching documents.
+ * @see #count(Query, Class, String)
+ * @see #estimatedCount(String)
+ * @see #exactCount(Query, String)
+ */
+ Mono count(Query query, String collectionName);
+
+ /**
+ * Returns the number of documents for the given {@link Query} by querying the given collection using the given entity
+ * class to map the given {@link Query}.
+ * NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
+ * influence on the resulting number of documents found as those values are passed on to the server and potentially
+ * limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
+ * count all matches.
+ * This method may choose to use {@link #estimatedCount(Class)} for empty queries instead of running an
+ * {@link com.mongodb.reactivestreams.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
+ * aggregation execution} which may have an impact on performance.
+ *
+ * @param query the {@link Query} class that specifies the criteria used to find documents. Must not be
+ * {@literal null}.
+ * @param entityClass the parametrized type. Can be {@literal null}.
+ * @param collectionName must not be {@literal null} or empty.
+ * @return the count of matching documents.
+ * @see #estimatedCount(String)
+ * @see #exactCount(Query, Class, String)
+ */
+ Mono count(Query query, @Nullable Class> entityClass, String collectionName);
+
+ /**
+ * Estimate the number of documents, in the collection {@link #getCollectionName(Class) identified by the given type},
+ * based on collection statistics.
+ * Please make sure to read the MongoDB reference documentation about limitations on eg. sharded cluster or inside
+ * transactions.
+ *
+ * @param entityClass must not be {@literal null}.
+ * @return a {@link Mono} emitting the estimated number of documents.
+ * @since 3.1
+ */
+ default Mono estimatedCount(Class> entityClass) {
+
+ Assert.notNull(entityClass, "Entity class must not be null!");
+ return estimatedCount(getCollectionName(entityClass));
+ }
+
+ /**
+ * Estimate the number of documents in the given collection based on collection statistics.
+ * Please make sure to read the MongoDB reference documentation about limitations on eg. sharded cluster or inside
+ * transactions.
+ *
+ * @param collectionName must not be {@literal null}.
+ * @return a {@link Mono} emitting the estimated number of documents.
+ * @since 3.1
+ */
+ Mono estimatedCount(String collectionName);
+
/**
* Returns the number of documents for the given {@link Query} by querying the collection of the given entity class.
*
@@ -937,92 +1025,6 @@ public interface ReactiveMongoOperations extends ReactiveFluentMongoOperations {
*/
Mono exactCount(Query query, @Nullable Class> entityClass, String collectionName);
- /**
- * Returns the number of documents for the given {@link Query} by querying the collection of the given entity class.
- *
- * NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
- * influence on the resulting number of documents found as those values are passed on to the server and potentially
- * limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
- * count all matches.
- *
- * This method may choose to use {@link #estimatedCount(Class)} for empty queries instead of running an
- * {@link com.mongodb.reactivestreams.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
- * aggregation execution} which may have an impact on performance.
- *
- * @param query the {@link Query} class that specifies the criteria used to find documents. Must not be
- * {@literal null}.
- * @param entityClass class that determines the collection to use. Must not be {@literal null}.
- * @return the count of matching documents.
- */
- Mono count(Query query, Class> entityClass);
-
- /**
- * Returns the number of documents for the given {@link Query} querying the given collection. The given {@link Query}
- * must solely consist of document field references as we lack type information to map potential property references
- * onto document fields. Use {@link #count(Query, Class, String)} to get full type specific support.
- * NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
- * influence on the resulting number of documents found as those values are passed on to the server and potentially
- * limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
- * count all matches.
- *
- * This method may choose to use {@link #estimatedCount(Class)} for empty queries instead of running an
- * {@link com.mongodb.reactivestreams.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
- * aggregation execution} which may have an impact on performance.
- *
- * @param query the {@link Query} class that specifies the criteria used to find documents.
- * @param collectionName must not be {@literal null} or empty.
- * @return the count of matching documents.
- * @see #count(Query, Class, String)
- */
- Mono count(Query query, String collectionName);
-
- /**
- * Returns the number of documents for the given {@link Query} by querying the given collection using the given entity
- * class to map the given {@link Query}.
- * NOTE: Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
- * influence on the resulting number of documents found as those values are passed on to the server and potentially
- * limit the range and order within which the server performs the count operation. Use an {@literal unpaged} query to
- * count all matches.
- *
- * This method may choose to use {@link #estimatedCount(Class)} for empty queries instead of running an
- * {@link com.mongodb.reactivestreams.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
- * aggregation execution} which may have an impact on performance.
- *
- * @param query the {@link Query} class that specifies the criteria used to find documents. Must not be
- * {@literal null}.
- * @param entityClass the parametrized type. Can be {@literal null}.
- * @param collectionName must not be {@literal null} or empty.
- * @return the count of matching documents.
- */
- Mono count(Query query, @Nullable Class> entityClass, String collectionName);
-
- /**
- * Estimate the number of documents, in the collection {@link #getCollectionName(Class) identified by the given type},
- * based on collection statistics.
- * Please make sure to read the MongoDB reference documentation about limitations on eg. sharded cluster or inside
- * transactions.
- *
- * @param entityClass must not be {@literal null}.
- * @return a {@link Mono} emitting the estimated number of documents.
- * @since 3.1
- */
- default Mono estimatedCount(Class> entityClass) {
-
- Assert.notNull(entityClass, "Entity class must not be null!");
- return estimatedCount(getCollectionName(entityClass));
- }
-
- /**
- * Estimate the number of documents in the given collection based on collection statistics.
- * Please make sure to read the MongoDB reference documentation about limitations on eg. sharded cluster or inside
- * transactions.
- *
- * @param collectionName must not be {@literal null}.
- * @return a {@link Mono} emitting the estimated number of documents.
- * @since 3.1
- */
- Mono estimatedCount(String collectionName);
-
/**
* Insert the object into the collection for the entity type of the object to save.
* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}.
diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java
index 9499fac9e..08e246714 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java
@@ -33,6 +33,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
+import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -367,10 +368,10 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
}
/**
- * En-/Disable usage of estimated count.
+ * Configure whether to use estimated count. Defaults to exact counting.
*
- * @param enabled if {@literal true} {@link com.mongodb.client.MongoCollection#estimatedDocumentCount()} ()} will we used for unpaged,
- * empty {@link Query queries}.
+ * @param enabled use {@link com.mongodb.client.MongoCollection#estimatedDocumentCount()} for unpaged and empty
+ * {@link Query queries} if {@code true}.
* @since 3.4
*/
public void useEstimatedCount(boolean enabled) {
@@ -378,11 +379,11 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
}
/**
- * En-/Disable usage of estimated count based on the given {@link BiFunction estimationFilter}.
+ * Configure whether to use estimated count based on the given {@link BiPredicate estimationFilter}.
*
- * @param enabled if {@literal true} {@link com.mongodb.client.MongoCollection#estimatedDocumentCount()} will we used for {@link Document
- * filter queries} that pass the given {@link BiFunction estimationFilter}.
- * @param estimationFilter the {@link BiFunction filter}.
+ * @param enabled use {@link com.mongodb.client.MongoCollection#estimatedDocumentCount()} for unpaged and empty
+ * {@link Query queries} if {@code true}.
+ * @param estimationFilter the {@link BiPredicate filter}.
* @since 3.4
*/
private void useEstimatedCount(boolean enabled, BiFunction> estimationFilter) {
@@ -1005,17 +1006,6 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
entityClass);
}
- @Override
- public Mono exactCount(Query query, @Nullable Class> entityClass, String collectionName) {
-
- CountContext countContext = queryOperations.countQueryContext(query);
-
- CountOptions options = countContext.getCountOptions(entityClass);
- Document mappedQuery = countContext.getMappedQuery(entityClass, mappingContext::getPersistentEntity);
-
- return doExactCount(collectionName, mappedQuery, options);
- }
-
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#count(org.springframework.data.mongodb.core.query.Query, java.lang.Class)
@@ -1052,11 +1042,6 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
});
}
- @Override
- public Mono estimatedCount(String collectionName) {
- return doEstimatedCount(collectionName, new EstimatedDocumentCountOptions());
- }
-
/**
* Run the actual count operation against the collection with given name.
*
@@ -1075,19 +1060,39 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
return countExecution.countDocuments(collectionName, filter, options);
}
- protected Mono doExactCount(String collectionName, Document filter, CountOptions options) {
-
- return createMono(collectionName,
- collection -> collection.countDocuments(CountQuery.of(filter).toQueryDocument(), options));
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#estimatedCount(java.lang.String)
+ */
+ @Override
+ public Mono estimatedCount(String collectionName) {
+ return doEstimatedCount(collectionName, new EstimatedDocumentCountOptions());
}
protected Mono doEstimatedCount(String collectionName, EstimatedDocumentCountOptions options) {
return createMono(collectionName, collection -> collection.estimatedDocumentCount(options));
}
+ @Override
+ public Mono exactCount(Query query, @Nullable Class> entityClass, String collectionName) {
+
+ CountContext countContext = queryOperations.countQueryContext(query);
+
+ CountOptions options = countContext.getCountOptions(entityClass);
+ Document mappedQuery = countContext.getMappedQuery(entityClass, mappingContext::getPersistentEntity);
+
+ return doExactCount(collectionName, mappedQuery, options);
+ }
+
+ protected Mono doExactCount(String collectionName, Document filter, CountOptions options) {
+
+ return createMono(collectionName,
+ collection -> collection.countDocuments(CountQuery.of(filter).toQueryDocument(), options));
+ }
+
protected Mono countCanBeEstimated(Document filter, CountOptions options) {
- if(!filter.isEmpty() || !isEmptyOptions(options)) {
+ if (!filter.isEmpty() || !isEmptyOptions(options)) {
return Mono.just(false);
}
return ReactiveMongoDatabaseUtils.isTransactionActive(getMongoDatabaseFactory()).map(it -> !it);