在 Spring Data MongoDB 中与众不同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19203724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Distinct in Spring Data MongoDB
提问by java_dude
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
?
有没有人尝试distinct
使用Spring Data for Mongo
. 如果你有一个例子,你可以发布它。我应该在哪里以及如何包括distinct flag
?
Link to the Spring Data Mongo example-- Example 4.4. Query creation from method names
链接到 Spring Data Mongo 示例——Example 4.4. Query creation from method names
// Enables the distinct flag for the query
List<Person> findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname);
List<Person> findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname);
采纳答案by java_dude
Lot has changed since this question was posted. Answering my own question as this question keeps popping up.
自从发布这个问题以来,很多事情都发生了变化。回答我自己的问题,因为这个问题不断出现。
Support is there since 3.0
and higher
从那时起就有支持,3.0
而且更高
public DistinctIterable<String> getUniqueTask() {
return mongoTemplate.getCollection(TABLE).distinct("FIELD", String.class);
}
Side Note: You can even add filters/regex to this query. Read docs. If you cannot find, ping, will post the answer.
旁注:您甚至可以向此查询添加过滤器/正则表达式。阅读文档。如果找不到,ping,将发布答案。
回答by paulscott56
After a little poking around, I have come up with the following solution, which is OK and works, but can probably be improved upon. I am still pretty new to Spring, so if you have a better idea, then please do let me know.
经过一番摸索,我想出了以下解决方案,它可以并且有效,但可能可以改进。我对 Spring 还是很陌生,所以如果你有更好的想法,请告诉我。
Anyway, here it is:
无论如何,这里是:
First off, we use the @Autowired
annotation to bring in the base MongoTemplate from spring-data-mongodb
首先,我们使用@Autowired
注解从 spring-data-mongodb 中引入基础 MongoTemplate
@Autowired
MongoTemplate mongoTemplate;
Once we have that, we can use it to make some queries. Note that this is the slightly smelly part because you have to tell Spring what the return type is and it doesn't really like that…
一旦我们有了它,我们就可以用它来进行一些查询。请注意,这是有点臭的部分,因为您必须告诉 Spring 返回类型是什么,而它并不真正喜欢那样......
// Get the distinct stuff from MongoDB
List<String> coll = mongoTemplate.getCollection("mycollection").distinct("myfield");
In the above code you will notice that I have defined a List type variable called coll that uses the @Autowired MongoTemplate
variable to get a collection and then a field using distinct. This is analogous to db.whatever.distinct("term")
on the Mongo shell.
在上面的代码中,您会注意到我定义了一个名为 coll 的 List 类型变量,它使用该@Autowired MongoTemplate
变量获取一个集合,然后使用 distinct 获取一个字段。这类似于db.whatever.distinct("term")
在 Mongo shell 上。
回答by iengchen
My environment: spring-data-mongodb 2.0.5,jdk1.8,
我的环境:spring-data-mongodb 2.0.5,jdk1.8,
Here is my code sample:
这是我的代码示例:
import com.mongodb.client.DistinctIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
public List<String> queryAllCategory() {
List<String> categoryList = new ArrayList<>();
MongoCollection mongoCollection = mongoTemplate.getCollection("lexicon");
DistinctIterable distinctIterable = mongoCollection.distinct("category",String.class);
MongoCursor cursor = distinctIterable.iterator();
while (cursor.hasNext()) {
String category = (String)cursor.next();
categoryList.add(category);
}
return categoryList;
}
about distinct method,please read: http://mongodb.github.io/mongo-java-driver/3.7/javadoc/com/mongodb/client/MongoCollection.html#distinct-java.lang.String-java.lang.Class-
关于不同的方法,请阅读:http: //mongodb.github.io/mongo-java-driver/3.7/javadoc/com/mongodb/client/MongoCollection.html#distinct-java.lang.String-java.lang.Class ——
回答by Oliver Drotbohm
Currently MongoDB does not support to retrieve documents in a distinct way. It only supports returning distinct field values using the distinct command.
目前 MongoDB 不支持以不同的方式检索文档。它只支持使用distinct 命令返回不同的字段值。
As it is apparently the latter you're looking for, the bad news is, we currently don't support any projections in derived queries. For progress on this, please follow the related JIRA ticket.
由于显然您正在寻找后者,坏消息是,我们目前不支持派生查询中的任何预测。有关这方面的进展,请关注相关的 JIRA票证。
回答by vmartin
You can see the differences of the usage of distinct between Spring Data JPA and Spring Data MongoDB here:
您可以在此处查看 Spring Data JPA 和 Spring Data MongoDB 之间使用 distinct 的差异:
@Before
public void setUp() {
this.dave = customers.save(new Customer("Dave", "Matthews"));
this.carter2 = customers.save(new Customer("Carter", "Z"));
this.carter = customers.save(new Customer("Carter", "Beauford"));
}
@Test
public void distinctProjectsEntityIntoInterface() {
Collection<CustomerProjection> result = customers.findAllProjectedDistinctBy();
assertThat(result, hasSize(2));
}
@Before
public void setUp() {
customers.deleteAll();
this.dave = customers.save(new Customer("Dave", "Matthews"));
this.carter2 = customers.save(new Customer("Carter", "Z"));
this.carter = customers.save(new Customer("Carter", "Beauford"));
}
@Test
public void distinctProjectsEntityIntoInterface() {
Collection<CustomerProjection> result = customers.findAllProjectedDistinctBy();
assertThat(result, hasSize(3));
}
distinct in spring data mongodb
where
在哪里
interface CustomerProjection {
String getFirstname();
}
回答by Arun Kumar
You can do it with the help of MongoOperations -
你可以在 MongoOperations 的帮助下做到这一点 -
Query query = new Query(where("field").in(requestIds));
List<String> result = mongoOperations.findDistinct(query, "fieldName", "collectionName", String.class);
With MongoTemplates -
使用 MongoTemplates -
mongoTemplate.getCollection("collectionName").distinct("filedName", requestIds);
回答by Nagaraja JB
If you want a list of distinct values in a list of Strings,
如果您想要字符串列表中的不同值列表,
List<String> emailIds = mongoTemplate.query(Person.class).distinct("email").as(String.class).all();