MongoDB differs with conditional in Java

Is it possible to implement MongoDB conditionally as described below, but with a Java driver?

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

      

I tried with MongoRepository

as 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 triedMongoTemplate

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

      

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

Regards

+3


source to share


2 answers


Spring MongoTemplate

has built-in support distinct

with query

:



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

+11


source


This might 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);

      

0


source







All Articles