计入Spring Data MongoDB存储库 [英] Count in Spring Data MongoDB repository

查看:86
本文介绍了计入Spring Data MongoDB存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何机制在Spring Data MongoDB存储库中使用 count 并使用 @Query 注释?
我很乐意收到我所拥有的文件数量而不必全部收到。

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.

基本上,相当于Java中的这个:

Basically, the equivalent of this in Java:

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


推荐答案

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

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);

这篇关于计入Spring Data MongoDB存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆