How to order the result from distinct list in Mong

2019-09-07 11:59发布

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条回答
祖国的老花朵
2楼-- · 2019-09-07 12:17

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.

查看更多
登录 后发表回答