Count in Spring Data MongoDB repository(在 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中的this:
Basically, the equivalent of this in Java:
db.test.find({"type":"foo"}).count
推荐答案
对我来说,这个解决方案就像一个魅力(使用 spring-data-mongodb 1.3.1.RELEASE ),我刚刚遇到了同样的问题并像这样解决了它(只是我工作代码中的一小段):
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 存储库中计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Spring Data MongoDB 存储库中计数


- C++ 和 Java 进程之间的共享内存 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01