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

耗尽温柔 提交于 2021-01-27 09:34:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!