How to order the result from distinct list in MongoDB via Java Driver?

情到浓时终转凉″ 提交于 2020-01-06 19:39:58

问题


I am using MongoDB via Java Driver (Spring Data MongoDB). Now my code is like:

DBObject query = new BasicDBObject("user", user);    
mongoTemplate.getCollection("reports").distinct("category", query);

Right now, the result list is not ordered. I want to it sorting by a ASC order. I have check this question Query MongoDB for ordered distinct values but I only get the answer which order the list by java side. How to order the result in MongoDB side?


回答1:


I don't think you can use the Distinct command with a "sort" parameter with standard queries.

However, you can use the aggregation framework to do these more complex queries.



来源:https://stackoverflow.com/questions/27476675/how-to-order-the-result-from-distinct-list-in-mongodb-via-java-driver

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