Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want result with distinct value of one field from mongodb using spring data

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);
like image 265
Laxmikant Kumbhare Avatar asked Feb 15 '23 06:02

Laxmikant Kumbhare


1 Answers

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)
like image 184
Mzzl Avatar answered Feb 16 '23 21:02

Mzzl