是否有人尝试使用distinct
在其查询中合并Spring Data for Mongo
。如果你有一个例子,请发表它。我应该在哪里以及如何包含distinct flag
?
Link to the Spring Data Mongo example - 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);
答案 0 :(得分:27)
经过一番探索之后,我提出了以下解决方案,这是好的并且有效,但可以改进。我对Spring很新,所以如果你有更好的想法,请告诉我。
无论如何,这是:
首先,我们使用@Autowired
注释从spring-data-mongodb引入基本的MongoTemplate
@Autowired
MongoTemplate mongoTemplate;
一旦我们有了这个,我们就可以用它来进行一些查询。请注意,这是一个稍微有点臭的部分,因为你必须告诉Spring返回类型是什么,它不是真的那样......
// Get the distinct stuff from MongoDB
List<String> coll = mongoTemplate.getCollection("mycollection").distinct("myfield");
在上面的代码中,您会注意到我已经定义了一个名为coll的List类型变量,该变量使用@Autowired MongoTemplate
变量来获取集合,然后使用distinct来使用字段。这类似于Mongo shell上的db.whatever.distinct("term")
。
答案 1 :(得分:3)
目前,MongoDB不支持以不同的方式检索文档。它仅支持使用distinct command返回不同的字段值。
因为显然后者正在寻找,坏消息是,我们目前不支持派生查询中的任何预测。有关此方面的进展,请按照相关的JIRA ticket。
进行操作答案 2 :(得分:3)
您可以在此处看到Spring Data JPA和Spring Data MongoDB之间的区别用法的差异:
@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
,其中
interface CustomerProjection {
String getFirstname();
}
答案 3 :(得分:3)
我的环境:spring-data-mongodb 2.0.5,jdk1.8,
这是我的代码示例:
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;
}
答案 4 :(得分:0)
自发布此问题以来,批次已更改。随着这个问题不断出现,回答了我自己的问题。
3.0
及更高版本开始提供支持
public DistinctIterable<String> getUniqueTask() {
return mongoTemplate.getCollection(TABLE).distinct("FIELD", String.class);
}
侧面注意:您甚至可以向此查询添加过滤器/正则表达式。阅读文档。如果找不到,请ping,将发布答案。
答案 5 :(得分:0)
您可以在MongoOperations的帮助下完成它-
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<script src="https://kit.fontawesome.com/bd579dd21a.js" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"
integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB"
crossorigin="anonymous"
/>
<title>My Navbar</title>
<body>
<nav class="navbar navbar-expand-sm navbar-dark bg-secondary">
<a href="#" class="navbar-brand">Control center</a>
<button class="navbar-toggler" data-toggle="collapse" data-target="#navbarMenu">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarMenu" role="tablist">
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" id="Monitor-tab" data-toggle="tab" data-target="#Monitor" href="#Monitor" role="tab" aria-expanded="false" aria-controls="Monitor" >Monitor</a>
<li class="nav-item">
<a class="nav-link" id="Configuration-tab" data-toggle="tab" data-target="#Configuration" href="#Configuration" aria-expanded="false" aria-controls="Configuration">Configuration</a>
</li>
<li class="nav-item">
<a class="nav-link" id="About-tab" data-toggle="tab" data-target="#About" href="#About" aria-expanded="false" aria-controls="About">About</a>
</li>
</ul>
</div>
</nav>
<div class="tab-content">
<div id="Monitor" class="tab-pane fade show active" role="tabpanel" aria-labelledby="Monitor-tab">
<h3>Monitor</h3>
<p>Values of the monitor.</p>
</div>
<div id="Configuration" class="tab-pane fade" role="tabpanel" aria-labelledby="Configuration-tab">
<p>Values of the configuration.</p>
</div>
<div id="About" class="tab-pane fade" role="tabpanel" aria-labelledby="About-tab">
<p>About my life.</p>
</div>
</div>
</body>
</html>
使用MongoTemplates-
Query query = new Query(where("field").in(requestIds));
List<String> result = mongoOperations.findDistinct(query, "fieldName", "collectionName", String.class);
答案 6 :(得分:0)
如果要在字符串列表中使用不同的值列表,
List<String> emailIds = mongoTemplate.query(Person.class).distinct("email").as(String.class).all();