Spring Data MongoDB 中的独特之处 [英] Distinct in Spring Data MongoDB

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

问题描述

有没有人尝试使用 Spring Data for Mongodistinct 合并到他们的查询中.如果您有示例,请发布.我应该在哪里以及如何包含 distinct flag?

Has anyone tried incorporating distinct in their query using Spring Data for Mongo. If you have an example can you please post it. Where and how should I include the distinct flag?

链接到Spring Data Mongo 示例 -- 示例 4.4.从方法名称创建查询

// Enables the distinct flag for the query
List<Person> findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname);
List<Person> findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname);

推荐答案

自从这个问题发布以来,很多事情都发生了变化.在这个问题不断出现时回答我自己的问题.

Lot has changed since this question was posted. Answering my own question as this question keeps popping up.

3.0 及更高版本开始提供支持

Support is there since 3.0 and higher

public DistinctIterable<String> getUniqueTask() {
    return mongoTemplate.getCollection(TABLE).distinct("FIELD", String.class);
}

旁注:您甚至可以在此查询中添加过滤器/正则表达式.阅读文档.如果找不到,ping,将发布答案.

这篇关于Spring Data MongoDB 中的独特之处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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