Frage

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);
War es hilfreich?

Lösung

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)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top