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 存储库中计数


基础教程推荐
- 多个组件的复杂布局 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01