I want result with distinct value of one field from mongodb using spring data. My code is
Query query = new Query();
query.addCriteria(Criteria.where("to.toId").in(toIdList));
query.fields().include("from");
query.fields().include("fromName");
query.fields().include("sentTime");
query.fields().include("hasAttachment");
newMessages = mongoTemplate.find(query, NewMessage.class);
You can use distinct instead of find:
db.mycollection.distinct('fieldname', query)
Using SpringData the syntax should be something like this:
mongoTemplate.getCollection(collection).distinct(key, query)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With