Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 828
Marcin Psyk Avatar asked Dec 14 '22 19:12

Marcin Psyk


1 Answers

Spring MongoTemplate has inbuilt support for distinct with query:

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

like image 65
Harsh Patel Avatar answered Jan 01 '23 14:01

Harsh Patel