Spring mongo repository slice(Spring mongo 存储库切片)
问题描述
我正在将 spring-sata-mongodb 1.8.2 与 MongoRepository 一起使用,并且我尝试在查询时使用 mongo $slice 选项来限制列表大小,但我在 mongorepository 中找不到此选项.
I am using spring-sata-mongodb 1.8.2 with MongoRepository and I am trying to use the mongo $slice option to limit a list size when query, but I can't find this option in the mongorepository.
我的课程如下所示:
public class InnerField{
public String a;
public String b;
public int n;
}
@Document(collection="Record")
punlic class Record{
public ObjectId id;
public List<InnerField> fields;
public int numer;
}
如您所见,我有一个集合名称Record",文档包含 InnerField.InnerField 列表一直在增长,所以我想在查询时限制所选字段的数量.
As you can see I have one collection name "Record" and the document contains the InnerField. the InnerField list is growing all the time so i want to limit the number of the selected fields when I am querying.
我看到了:https://docs.mongodb.org/v3.0/tutorial/project-fields-from-query-results/
这正是我需要的,但我在 mongorepository 中找不到相关参考.
which is exactly what I need but I couldn't find the relevant reference in mongorepository.
有什么想法吗?
推荐答案
使用 Java Mongo 驱动程序中提供的切片功能,使用投影,如下代码所示.
Use slice functionality as provided in Java Mongo driver using projection as in below code.
例如:
List<Entity> list = new ArrayList<Entity>();
// Return the last 10 weeks data only
FindIterable<Document> list = db.getDBCollection("COLLECTION").find()
.projection(Projections.fields(Projections.slice("count", -10)));
MongoCursor<Document> doc = list.iterator();
while(doc.hasNext()){
list.add(new Gson().fromJson(doc.next().toJson(), Entity.class));
}
上述查询将获取所有实体类类型的文档,并且每个实体类文档的字段"列表将只有最后 10 条记录.
The above query will fetch all documents of type Entity class and the "field" list of each Entity class document will have only last 10 records.
这篇关于Spring mongo 存储库切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Spring mongo 存储库切片


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