繁体   English   中英

如何使用 Spring Data Mongo DB 仅检索文档的特定字段?

[英]How to retrieve only specific field of a document with Spring Data Mongo DB?

例如,我有一个包含以下文档的集合:

class Group {
  @Id
  @Field( '_id' )
  UUID id

  @Field( 'title' )
  String title
}

我有一个存储库: interface GroupRepository extends MongoRepository<Group, UUID> { }如何检索所有标识符的列表? 我不想检索所有文档,然后收集 ID。 我想要一个方法,如: List<UUID> findAllGroupIds() 如果没有Criteria显式实现,是否有可能? 我尝试使用@Query ,但没有给出结果。

您可以使用 MongoDB 聚合框架来检索 ID 列表。

Aggregation agg = newAggregation(stages.add(project("_id")));

AggregationResults<UUID> ids = mongoOperation.aggregate(agg,
                "Group", UUID.class);

暂无
暂无

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

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