Count in Spring Data MongoDB repository

spring data mongodb aggregation count
spring data mongodb query annotation
spring data mongodb join query
spring data mongodb like query
spring boot mongodb custom query
spring data mongodb properties
spring data mongodb id
spring data mongodb stream

I wonder if there's any mechanism to use count in Spring Data MongoDB repository with @Query annotation? I would love to receive the number of documents that I have without having to get all of them.

Basically, the equivalent of this in Java:


For me this solutions works like a charm( using spring-data-mongodb 1.3.1.RELEASE ), I just had the same problem atm and solved it like this(just a short snippet from my code at work):

@Query(value = "{'productDetails.productType': {$regex: ?0, $options: 'i'}, 'sourceDescriptor': ?1}", count = true)
public Long countFetchedDocumentsForCategory(String cat, String sourceDescriptor);

How to get mongodb document count for matched query using , This mongodb and spring data tutorial will help to learn about how to get the count of documents match by query criteria. Spring Data MongoDB Repositories Examples of how to set up the MongoRepository to read and update MongoDB. Shows the count, exists, find, save and delete methods, plus paging and sorting.

Another way to do this using MongoRepository query templates:

public interface MyRepository extends MongoRepository<MyClass, String> {
    Long countByLastname(String lastname);


CountQuery (Spring Data MongoDB 2.2.6.RELEASE API), Annotation to declare finder count queries directly on repository methods. Both attributes allow using a placeholder notation of ?0 , ?1 and so on. Since: 1.10  This namespace element will cause the base packages to be scanned for interfaces extending MongoRepository and create Spring beans for each of them found. By default the repositories will get a MongoTemplate Spring bean wired that is called mongoTemplate, so you only need to configure mongo-template-ref explicitly if you deviate from this convention.

I had same problem recently and unfortunately did not find any solution at least not with current stable version. It seems that it is possible in Spring Data JPA 1.4M1 so maybe it will be also included in next version of Spring Data MongoDB.

6. MongoDB repositories, This chapter will point out the specialties for repository support for MongoDB. <​bean id="mongoTemplate" class=" Page<T> findAll(Predicate predicate, Pageable pageable); Long count(Predicate​  I have figured this out finally. The key is to use count() aggregation function in Spring Data for MongoDB, which translates to sum increments by 1 in native Mongo shell.

Spring Data MongoDB Queries, In this article, we will learn about Spring data MongoDB queries with different ways to Auto Generated Methods with Mongo Repositories setSalary(4563 + (count * 10 + 45 + count)); return employee; }).collect(Collectors. In MongoDB, Projections are a way to fetch only the required fields of a document from a database. This reduces the amount of data that has to be transferred from database server to client and hence increases performance. With Spring Data MongDB, projections can be used both with MongoTemplate and MongoRepository. java , Best Java code snippets using​Query.count (Showing top 4 results out of 315). Add the Codota plugin to your IDE  Spring MVC Tutorial Our Spring MVC tutorial is designed for Java programmers with a need to understand the Spring MVC along with its architecture and actual usage. Spring Boot Tutorials Our Spring Boot tutorials covers the core and advances feature of Spring Boot including Starters, Actuator, CLI and Spring Boot build process. , Spring Data : Java configuration for MongoDB without XML. @Repository public class PersonRepository { @Autowired MongoTemplate mongoTemplate; public  Spring Data repositories are implemented by using fragments that form a repository composition. Fragments are the base repository, functional aspects (such as QueryDsl), and custom interfaces along with their implementation. Each time you add an interface to your repository interface, you enhance the composition by adding a fragment.