简体   繁体   English

计入Spring Data MongoDB存储库

[英]Count in Spring Data MongoDB repository

I wonder if there's any mechanism to use count in Spring Data MongoDB repository with @Query annotation? 我想知道是否有任何机制在Spring Data MongoDB存储库中使用@Query注释count 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: 基本上,相当于Java中的这个:

db.test.find({"type":"foo"}).count 

Another way to do this using MongoRepository query templates: 使用MongoRepository查询模板执行此操作的另一种方法:

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

See http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repositories.query-methods.details http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repositories.query-methods.details

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): 对我来说,这个解决方案就像一个魅力(使用spring-data-mongodb 1.3.1.RELEASE),我只是遇到了同样的问题atm并且像这样解决了它(只是我工作代码中的一小段代码):

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

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. 看起来它可能在Spring Data JPA 1.4M1中,所以也许它也将包含在下一版本的Spring Data MongoDB中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM