2014-10-29 54 views
3

有沒有可能實現MongoDB與條件不同,如下所示,但與Java驅動程序?MongoDB與Java中的條件不一致

db.orders.distinct('ord_dt', { price: { $gt: 10 } }) 

我試圖與MongoRepository,像下面

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

List<Person> findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname); 

但在我看來,它不能正常工作。我也試過MongoTemplate

mongoTemplate.getCollection("mycollection").distinct("myfield") 

但是沒有辦法實現條件。任何想法如何解決?

致以問候

回答

11

MongoTemplatedistinctquery內置支持:

mongoTemplate.getCollection("collection_name").distinct("field", new BasicDBObject("price", new BasicDBObject("$gt", 10)));

+0

感謝兄弟的工作........... – 2014-10-29 12:07:36

0

這可能會對您有所幫助。

BasicDBObject match = new BasicDBObject(); 
match.put("$query", new BasicDBObject("price", new BasicDBObject("$gt", 10))); 
List list = mongoTemplate.getCollection("mycollection").distinct("ord_dt", match); 
+0

OK,謝謝。另一個問題是,如何將它與Pageable接口結合以提供分頁結果? – 2014-10-29 12:00:27

+0

您可以使用此[鏈接](http://docs.spring.io/spring-data/data-mongo/docs/1.4.3.RELEASE/reference/html/mongo.repositories.html) – 2014-10-29 12:04:59

+0

請參考[鏈接] ] http://stackoverflow.com/q/25399413/2823164問題 – 2014-10-29 12:18:32