Polishing.
Reorder methods. Add links to Javadoc. Tweak wording. See: #3522 Original pull request: #3951.
This commit is contained in:
@@ -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
|
* 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
|
* a useful option for extensibility and testability (as it can be easily mocked, stubbed, or be the target of a JDK
|
||||||
* proxy).
|
* proxy). <br />
|
||||||
* <br />
|
|
||||||
* <strong>NOTE:</strong> Some operations cannot be executed within a MongoDB transaction. Please refer to the MongoDB
|
* <strong>NOTE:</strong> Some operations cannot be executed within a MongoDB transaction. Please refer to the MongoDB
|
||||||
* specific documentation to learn more about <a href="https://docs.mongodb.com/manual/core/transactions/">Multi
|
* specific documentation to learn more about <a href="https://docs.mongodb.com/manual/core/transactions/">Multi
|
||||||
* Document Transactions</a>.
|
* Document Transactions</a>.
|
||||||
@@ -82,7 +81,7 @@ public interface MongoOperations extends FluentMongoOperations {
|
|||||||
String getCollectionName(Class<?> entityClass);
|
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
|
* 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.
|
* converted into Spring's DAO exception hierarchy.
|
||||||
*
|
*
|
||||||
@@ -122,8 +121,7 @@ public interface MongoOperations extends FluentMongoOperations {
|
|||||||
void executeQuery(Query query, String collectionName, DocumentCallbackHandler dch);
|
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. <br />
|
||||||
* <br />
|
|
||||||
* Allows for returning a result object, that is a domain object or a collection of domain objects.
|
* 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
|
* @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> T execute(DbCallback<T> action);
|
<T> T execute(DbCallback<T> 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. <br />
|
||||||
* <br />
|
|
||||||
* Allows for returning a result object, that is a domain object or a collection of domain objects.
|
* 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}.
|
* @param entityClass class that determines the collection to use. Must not be {@literal null}.
|
||||||
@@ -148,8 +145,7 @@ public interface MongoOperations extends FluentMongoOperations {
|
|||||||
<T> T execute(Class<?> entityClass, CollectionCallback<T> action);
|
<T> T execute(Class<?> entityClass, CollectionCallback<T> 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. <br />
|
||||||
* <br />
|
|
||||||
* Allows for returning a result object, that is a domain object or a collection of domain objects.
|
* 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
|
* @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}
|
* 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. <br />
|
||||||
* <br />
|
|
||||||
* <strong>Note:</strong> It is up to the caller to manage the {@link ClientSession} lifecycle. Use the
|
* <strong>Note:</strong> 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}.
|
* {@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}. <br />
|
||||||
* <br />
|
|
||||||
* <strong>Note:</strong> It is up to the caller to manage the {@link ClientSession} lifecycle.
|
* <strong>Note:</strong> It is up to the caller to manage the {@link ClientSession} lifecycle.
|
||||||
*
|
*
|
||||||
* @param session must not be {@literal null}.
|
* @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
|
* 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
|
* 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)
|
* {@link #createCollection(Class)}. Please make sure to check if the collection {@link #collectionExists(Class)
|
||||||
* exists} first.
|
* exists} first. <br />
|
||||||
* <br />
|
|
||||||
* Translate any exceptions as necessary.
|
* Translate any exceptions as necessary.
|
||||||
*
|
*
|
||||||
* @param collectionName name of the collection. Must not be {@literal null}.
|
* @param collectionName name of the collection. Must not be {@literal null}.
|
||||||
@@ -307,8 +300,7 @@ public interface MongoOperations extends FluentMongoOperations {
|
|||||||
MongoCollection<Document> getCollection(String collectionName);
|
MongoCollection<Document> 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. <br />
|
||||||
* <br />
|
|
||||||
* Translate any exceptions as necessary.
|
* Translate any exceptions as necessary.
|
||||||
*
|
*
|
||||||
* @param entityClass class that determines the name of the collection. Must not be {@literal null}.
|
* @param entityClass class that determines the name of the collection. Must not be {@literal null}.
|
||||||
@@ -317,8 +309,7 @@ public interface MongoOperations extends FluentMongoOperations {
|
|||||||
<T> boolean collectionExists(Class<T> entityClass);
|
<T> boolean collectionExists(Class<T> entityClass);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check to see if a collection with a given name exists.
|
* Check to see if a collection with a given name exists. <br />
|
||||||
* <br />
|
|
||||||
* Translate any exceptions as necessary.
|
* Translate any exceptions as necessary.
|
||||||
*
|
*
|
||||||
* @param collectionName name of the collection. Must not be {@literal null}.
|
* @param collectionName name of the collection. Must not be {@literal null}.
|
||||||
@@ -327,8 +318,7 @@ public interface MongoOperations extends FluentMongoOperations {
|
|||||||
boolean collectionExists(String collectionName);
|
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. <br />
|
||||||
* <br />
|
|
||||||
* Translate any exceptions as necessary.
|
* Translate any exceptions as necessary.
|
||||||
*
|
*
|
||||||
* @param entityClass class that determines the collection to drop/delete. Must not be {@literal null}.
|
* @param entityClass class that determines the collection to drop/delete. Must not be {@literal null}.
|
||||||
@@ -336,8 +326,7 @@ public interface MongoOperations extends FluentMongoOperations {
|
|||||||
<T> void dropCollection(Class<T> entityClass);
|
<T> void dropCollection(Class<T> entityClass);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Drop the collection with the given name.
|
* Drop the collection with the given name. <br />
|
||||||
* <br />
|
|
||||||
* Translate any exceptions as necessary.
|
* Translate any exceptions as necessary.
|
||||||
*
|
*
|
||||||
* @param collectionName name of the collection to drop/delete.
|
* @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);
|
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. <br />
|
||||||
* <br />
|
|
||||||
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless
|
* 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. <br />
|
||||||
* <br />
|
|
||||||
* If your collection does not contain a homogeneous collection of types, this operation will not be an efficient way
|
* 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.
|
* 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 {
|
|||||||
<T> List<T> findAll(Class<T> entityClass);
|
<T> List<T> findAll(Class<T> 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. <br />
|
||||||
* <br />
|
|
||||||
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless
|
* 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. <br />
|
||||||
* <br />
|
|
||||||
* If your collection does not contain a homogeneous collection of types, this operation will not be an efficient way
|
* 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.
|
* 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
|
* 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. <br />
|
||||||
* <br />
|
|
||||||
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless
|
* 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. <br />
|
||||||
* <br />
|
|
||||||
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
||||||
* feature rich {@link Query}.
|
* 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
|
* Map the results of an ad-hoc query on the specified collection to a single instance of an object of the specified
|
||||||
* type.
|
* type. <br />
|
||||||
* <br />
|
|
||||||
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless
|
* 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. <br />
|
||||||
* <br />
|
|
||||||
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
||||||
* feature rich {@link Query}.
|
* feature rich {@link Query}.
|
||||||
*
|
*
|
||||||
@@ -740,11 +721,9 @@ public interface MongoOperations extends FluentMongoOperations {
|
|||||||
boolean exists(Query query, @Nullable Class<?> entityClass, String collectionName);
|
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. <br />
|
||||||
* <br />
|
|
||||||
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless
|
* 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. <br />
|
||||||
* <br />
|
|
||||||
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
||||||
* feature rich {@link Query}.
|
* feature rich {@link Query}.
|
||||||
*
|
*
|
||||||
@@ -756,11 +735,9 @@ public interface MongoOperations extends FluentMongoOperations {
|
|||||||
<T> List<T> find(Query query, Class<T> entityClass);
|
<T> List<T> find(Query query, Class<T> 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. <br />
|
||||||
* <br />
|
|
||||||
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless
|
* 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. <br />
|
||||||
* <br />
|
|
||||||
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
||||||
* feature rich {@link Query}.
|
* 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
|
* 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
|
* specified type. The first document that matches the query is returned and also removed from the collection in the
|
||||||
* database.
|
* database. <br />
|
||||||
* <br />
|
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. <br />
|
||||||
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}.
|
|
||||||
* <br />
|
|
||||||
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
||||||
* feature rich {@link Query}.
|
* 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.
|
* type. The first document that matches the query is returned and also removed from the collection in the database.
|
||||||
* <br />
|
* <br />
|
||||||
* The object is converted from the MongoDB native representation using an instance of {@see MongoConverter}. Unless
|
* 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. <br />
|
||||||
* <br />
|
|
||||||
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
* The query is specified as a {@link Query} which can be created either using the {@link BasicQuery} or the more
|
||||||
* feature rich {@link Query}.
|
* feature rich {@link Query}.
|
||||||
*
|
*
|
||||||
@@ -1132,8 +1106,96 @@ public interface MongoOperations extends FluentMongoOperations {
|
|||||||
* <strong>NOTE:</strong> Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
|
* <strong>NOTE:</strong> 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
|
* 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
|
* 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. <br />
|
||||||
|
* 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. <br />
|
||||||
|
* <strong>NOTE:</strong> 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. <br />
|
||||||
|
* 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}. <br />
|
||||||
|
* <strong>NOTE:</strong> 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. <br />
|
||||||
|
* 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. <br />
|
||||||
|
* 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. <br />
|
||||||
|
* 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.
|
||||||
* <br />
|
* <br />
|
||||||
|
* <strong>NOTE:</strong> 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. <br />
|
||||||
* This method uses an
|
* This method uses an
|
||||||
* {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
|
* {@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
|
* 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 {
|
|||||||
* <strong>NOTE:</strong> Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
|
* <strong>NOTE:</strong> 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
|
* 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
|
* 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. <br />
|
||||||
* <br />
|
|
||||||
* This method uses an
|
* This method uses an
|
||||||
* {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
|
* {@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
|
* 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 {
|
|||||||
* <strong>NOTE:</strong> Query {@link Query#getSkip() offset} and {@link Query#getLimit() limit} can have direct
|
* <strong>NOTE:</strong> 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
|
* 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
|
* 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. <br />
|
||||||
* <br />
|
|
||||||
* This method uses an
|
* This method uses an
|
||||||
* {@link com.mongodb.client.MongoCollection#countDocuments(org.bson.conversions.Bson, com.mongodb.client.model.CountOptions)
|
* {@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
|
* 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);
|
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.
|
* Insert the object into the collection for the entity type of the object to save. <br />
|
||||||
* <br />
|
* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. <br />
|
||||||
* <strong>NOTE:</strong> 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.
|
|
||||||
* <br />
|
|
||||||
* 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. <br />
|
|
||||||
* <strong>NOTE:</strong> 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.
|
|
||||||
* <br />
|
|
||||||
* 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.
|
|
||||||
* <br />
|
|
||||||
* 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.
|
|
||||||
* <br />
|
|
||||||
* 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}. <br />
|
|
||||||
* <strong>NOTE:</strong> 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.
|
|
||||||
* <br />
|
|
||||||
* 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.
|
|
||||||
* <br />
|
|
||||||
* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}.
|
|
||||||
* <br />
|
|
||||||
* If your object has an "Id' property, it will be set with the generated Id from MongoDB. If your Id property is a
|
* 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
|
* 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
|
* property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
|
||||||
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation" > Spring's
|
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation" > Spring's
|
||||||
* Type Conversion"</a> for more details.
|
* Type Conversion"</a> for more details. <br />
|
||||||
* <br />
|
|
||||||
* Insert is used to initially store the object into the database. To update an existing object use the save method.
|
* Insert is used to initially store the object into the database. To update an existing object use the save method.
|
||||||
* <br />
|
* <br />
|
||||||
* The {@code objectToSave} must not be collection-like.
|
* The {@code objectToSave} must not be collection-like.
|
||||||
@@ -1308,11 +1277,9 @@ public interface MongoOperations extends FluentMongoOperations {
|
|||||||
<T> T insert(T objectToSave);
|
<T> T insert(T objectToSave);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert the object into the specified collection.
|
* Insert the object into the specified collection. <br />
|
||||||
* <br />
|
|
||||||
* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. Unless
|
* 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. <br />
|
||||||
* <br />
|
|
||||||
* Insert is used to initially store the object into the database. To update an existing object use the save method.
|
* Insert is used to initially store the object into the database. To update an existing object use the save method.
|
||||||
* <br />
|
* <br />
|
||||||
* The {@code objectToSave} must not be collection-like.
|
* 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
|
* 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'. <br />
|
||||||
* <br />
|
|
||||||
* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. Unless
|
* 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. <br />
|
||||||
* <br />
|
|
||||||
* If your object has an "Id' property, it will be set with the generated Id from MongoDB. If your Id property is a
|
* 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
|
* 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
|
* property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
|
||||||
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation" > Spring's
|
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation" > Spring's
|
||||||
* Type Conversion"</a> for more details.
|
* Type Conversion"</a> for more details. <br />
|
||||||
* <br />
|
|
||||||
* The {@code objectToSave} must not be collection-like.
|
* The {@code objectToSave} must not be collection-like.
|
||||||
*
|
*
|
||||||
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
|
* @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
|
* 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'. <br />
|
||||||
* <br />
|
|
||||||
* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. Unless
|
* 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. <br />
|
||||||
* <br />
|
|
||||||
* If your object has an "Id' property, it will be set with the generated Id from MongoDB. If your Id property is a
|
* 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
|
* 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.
|
* property type will be handled by Spring's BeanWrapper class that leverages Type Conversion API. See
|
||||||
* See <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation">Spring's Type Conversion</a> for more details.
|
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#validation">Spring's Type
|
||||||
* <br />
|
* Conversion</a> for more details. <br />
|
||||||
* The {@code objectToSave} must not be collection-like.
|
* The {@code objectToSave} must not be collection-like.
|
||||||
*
|
*
|
||||||
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
|
* @param objectToSave the object to store in the collection. Must not be {@literal null}.
|
||||||
|
|||||||
@@ -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,
|
* @param enabled use {@link com.mongodb.client.MongoCollection#estimatedDocumentCount()} for unpaged and empty
|
||||||
* empty {@link Query queries}.
|
* {@link Query queries} if {@code true}.
|
||||||
* @since 3.4
|
* @since 3.4
|
||||||
*/
|
*/
|
||||||
public void useEstimatedCount(boolean enabled) {
|
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
|
* @param enabled use {@link com.mongodb.client.MongoCollection#estimatedDocumentCount()} for unpaged and empty
|
||||||
* filter queries} that pass the given {@link BiPredicate estimationFilter}.
|
* {@link Query queries} if {@code true}.
|
||||||
* @param estimationFilter the {@link BiPredicate filter}.
|
* @param estimationFilter the {@link BiPredicate filter}.
|
||||||
* @since 3.4
|
* @since 3.4
|
||||||
*/
|
*/
|
||||||
@@ -1013,17 +1013,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
|||||||
return count(query, null, collectionName);
|
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)
|
* (non-Javadoc)
|
||||||
* @see org.springframework.data.mongodb.core.MongoOperations#count(org.springframework.data.mongodb.core.query.Query, java.lang.Class, java.lang.String)
|
* @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);
|
return doCount(collectionName, mappedQuery, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
|
||||||
protected long doCount(String collectionName, Document filter, CountOptions options) {
|
protected long doCount(String collectionName, Document filter, CountOptions options) {
|
||||||
|
|
||||||
if (LOGGER.isDebugEnabled()) {
|
if (LOGGER.isDebugEnabled()) {
|
||||||
@@ -1052,6 +1040,30 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
|||||||
return countExecution.countDocuments(collectionName, filter, options);
|
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) {
|
protected long doExactCount(String collectionName, Document filter, CountOptions options) {
|
||||||
return execute(collectionName,
|
return execute(collectionName,
|
||||||
collection -> collection.countDocuments(CountQuery.of(filter).toQueryDocument(), options));
|
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;
|
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
|
@Override
|
||||||
public <T> T insert(T objectToSave) {
|
public <T> T insert(T objectToSave) {
|
||||||
|
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ public interface ReactiveMongoOperations extends ReactiveFluentMongoOperations {
|
|||||||
ReactiveIndexOperations indexOps(Class<?> entityClass);
|
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
|
* 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.
|
* 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}. <br />
|
* Obtain a {@link ClientSession} bound instance of {@link ReactiveMongoOperations}. <br />
|
||||||
* <strong>Note:</strong> It is up to the caller to manage the {@link ClientSession} lifecycle.
|
* <strong>Note:</strong> 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}.
|
* @return {@link ClientSession} bound instance of {@link ReactiveMongoOperations}.
|
||||||
* @since 2.1
|
* @since 2.1
|
||||||
*/
|
*/
|
||||||
@@ -868,6 +867,95 @@ public interface ReactiveMongoOperations extends ReactiveFluentMongoOperations {
|
|||||||
*/
|
*/
|
||||||
<T> Mono<T> findAndRemove(Query query, Class<T> entityClass, String collectionName);
|
<T> Mono<T> findAndRemove(Query query, Class<T> entityClass, String collectionName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of documents for the given {@link Query} by querying the collection of the given entity class.
|
||||||
|
* <br />
|
||||||
|
* <strong>NOTE:</strong> 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. <br />
|
||||||
|
* 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<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. <br />
|
||||||
|
* <strong>NOTE:</strong> 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. <br />
|
||||||
|
* 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<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}. <br />
|
||||||
|
* <strong>NOTE:</strong> 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. <br />
|
||||||
|
* 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<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. <br />
|
||||||
|
* 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<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. <br />
|
||||||
|
* 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<Long> estimatedCount(String collectionName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of documents for the given {@link Query} by querying the collection of the given entity class.
|
* Returns the number of documents for the given {@link Query} by querying the collection of the given entity class.
|
||||||
* <br />
|
* <br />
|
||||||
@@ -937,92 +1025,6 @@ public interface ReactiveMongoOperations extends ReactiveFluentMongoOperations {
|
|||||||
*/
|
*/
|
||||||
Mono<Long> exactCount(Query query, @Nullable Class<?> entityClass, String collectionName);
|
Mono<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.
|
|
||||||
* <br />
|
|
||||||
* <strong>NOTE:</strong> 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.
|
|
||||||
* <br />
|
|
||||||
* 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<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. <br />
|
|
||||||
* <strong>NOTE:</strong> 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.
|
|
||||||
* <br />
|
|
||||||
* 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<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}. <br />
|
|
||||||
* <strong>NOTE:</strong> 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.
|
|
||||||
* <br />
|
|
||||||
* 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<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. <br />
|
|
||||||
* 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<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. <br />
|
|
||||||
* 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<Long> estimatedCount(String collectionName);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert the object into the collection for the entity type of the object to save. <br />
|
* Insert the object into the collection for the entity type of the object to save. <br />
|
||||||
* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. <br />
|
* The object is converted to the MongoDB native representation using an instance of {@see MongoConverter}. <br />
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import java.util.Map;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
|
import java.util.function.BiPredicate;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
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,
|
* @param enabled use {@link com.mongodb.client.MongoCollection#estimatedDocumentCount()} for unpaged and empty
|
||||||
* empty {@link Query queries}.
|
* {@link Query queries} if {@code true}.
|
||||||
* @since 3.4
|
* @since 3.4
|
||||||
*/
|
*/
|
||||||
public void useEstimatedCount(boolean enabled) {
|
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
|
* @param enabled use {@link com.mongodb.client.MongoCollection#estimatedDocumentCount()} for unpaged and empty
|
||||||
* filter queries} that pass the given {@link BiFunction estimationFilter}.
|
* {@link Query queries} if {@code true}.
|
||||||
* @param estimationFilter the {@link BiFunction filter}.
|
* @param estimationFilter the {@link BiPredicate filter}.
|
||||||
* @since 3.4
|
* @since 3.4
|
||||||
*/
|
*/
|
||||||
private void useEstimatedCount(boolean enabled, BiFunction<Document, CountOptions, Mono<Boolean>> estimationFilter) {
|
private void useEstimatedCount(boolean enabled, BiFunction<Document, CountOptions, Mono<Boolean>> estimationFilter) {
|
||||||
@@ -1005,17 +1006,6 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
|||||||
entityClass);
|
entityClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Mono<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)
|
* (non-Javadoc)
|
||||||
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#count(org.springframework.data.mongodb.core.query.Query, java.lang.Class)
|
* @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<Long> estimatedCount(String collectionName) {
|
|
||||||
return doEstimatedCount(collectionName, new EstimatedDocumentCountOptions());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the actual count operation against the collection with given name.
|
* 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);
|
return countExecution.countDocuments(collectionName, filter, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Mono<Long> doExactCount(String collectionName, Document filter, CountOptions options) {
|
/*
|
||||||
|
* (non-Javadoc)
|
||||||
return createMono(collectionName,
|
* @see org.springframework.data.mongodb.core.ReactiveMongoOperations#estimatedCount(java.lang.String)
|
||||||
collection -> collection.countDocuments(CountQuery.of(filter).toQueryDocument(), options));
|
*/
|
||||||
|
@Override
|
||||||
|
public Mono<Long> estimatedCount(String collectionName) {
|
||||||
|
return doEstimatedCount(collectionName, new EstimatedDocumentCountOptions());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Mono<Long> doEstimatedCount(String collectionName, EstimatedDocumentCountOptions options) {
|
protected Mono<Long> doEstimatedCount(String collectionName, EstimatedDocumentCountOptions options) {
|
||||||
return createMono(collectionName, collection -> collection.estimatedDocumentCount(options));
|
return createMono(collectionName, collection -> collection.estimatedDocumentCount(options));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<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 Mono<Long> doExactCount(String collectionName, Document filter, CountOptions options) {
|
||||||
|
|
||||||
|
return createMono(collectionName,
|
||||||
|
collection -> collection.countDocuments(CountQuery.of(filter).toQueryDocument(), options));
|
||||||
|
}
|
||||||
|
|
||||||
protected Mono<Boolean> countCanBeEstimated(Document filter, CountOptions options) {
|
protected Mono<Boolean> countCanBeEstimated(Document filter, CountOptions options) {
|
||||||
|
|
||||||
if(!filter.isEmpty() || !isEmptyOptions(options)) {
|
if (!filter.isEmpty() || !isEmptyOptions(options)) {
|
||||||
return Mono.just(false);
|
return Mono.just(false);
|
||||||
}
|
}
|
||||||
return ReactiveMongoDatabaseUtils.isTransactionActive(getMongoDatabaseFactory()).map(it -> !it);
|
return ReactiveMongoDatabaseUtils.isTransactionActive(getMongoDatabaseFactory()).map(it -> !it);
|
||||||
|
|||||||
Reference in New Issue
Block a user