简体   繁体   中英

MongoDB Distinct with condition in Java

Is there any possibility to implement MongoDB distinct with condition, like this below, but with Java driver?

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

I have tried with MongoRepository , like below

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

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

But in my opinion it is not working correctly. I have also tried MongoTemplate

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

But there is no way to implement condition. Any idea how to solve that?

Best regards

Spring MongoTemplate内置了对query distinct支持:

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

This may help you.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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