Search in sources :

Example 1 with CountContext

use of org.springframework.data.mongodb.core.QueryOperations.CountContext in project spring-data-mongodb by spring-projects.

the class MongoTemplate method count.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.MongoOperations#count(org.springframework.data.mongodb.core.query.Query, java.lang.Class, java.lang.String)
	 */
public long count(Query query, @Nullable Class<?> entityClass, String collectionName) {
    Assert.notNull(query, "Query must not be null!");
    Assert.hasText(collectionName, "Collection name must not be null or empty!");
    CountContext countContext = queryOperations.countQueryContext(query);
    CountOptions options = countContext.getCountOptions(entityClass);
    Document mappedQuery = countContext.getMappedQuery(entityClass, mappingContext::getPersistentEntity);
    return doCount(collectionName, mappedQuery, options);
}
Also used : CountContext(org.springframework.data.mongodb.core.QueryOperations.CountContext) Document(org.bson.Document)

Example 2 with CountContext

use of org.springframework.data.mongodb.core.QueryOperations.CountContext in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplate method count.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#count(org.springframework.data.mongodb.core.query.Query, java.lang.Class, java.lang.String)
	 */
public Mono<Long> count(Query query, @Nullable Class<?> entityClass, String collectionName) {
    Assert.notNull(query, "Query must not be null!");
    Assert.hasText(collectionName, "Collection name must not be null or empty!");
    return createMono(collectionName, collection -> {
        CountContext countContext = queryOperations.countQueryContext(query);
        CountOptions options = countContext.getCountOptions(entityClass);
        Document filter = countContext.getMappedQuery(entityClass, mappingContext::getPersistentEntity);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(String.format("Executing count: %s in collection: %s", serializeToJsonSafely(filter), collectionName));
        }
        return doCount(collectionName, filter, options);
    });
}
Also used : CountContext(org.springframework.data.mongodb.core.QueryOperations.CountContext) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument)

Aggregations

Document (org.bson.Document)2 CountContext (org.springframework.data.mongodb.core.QueryOperations.CountContext)2 FullDocument (com.mongodb.client.model.changestream.FullDocument)1