问题
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);
回答1:
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)
来源:https://stackoverflow.com/questions/20835905/i-want-result-with-distinct-value-of-one-field-from-mongodb-using-spring-data