如何通过 Java Driver 从 MongoDB 中的不同列表中排序结果? [英] How to order the result from distinct list in MongoDB via Java Driver?

查看:43
本文介绍了如何通过 Java Driver 从 MongoDB 中的不同列表中排序结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 Java 驱动程序(Spring Data MongoDB)使用 MongoDB.现在我的代码是这样的:

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

现在,结果列表没有排序.我想按 ASC 顺序排序.我已经检查了这个问题查询 MongoDB 以获得有序的不同值 但我只得到回答 java 端对列表的排序.MongoDB端如何对结果进行排序?

解决方案

我不认为你可以使用 不同命令 带有标准查询的排序"参数.

但是,您可以使用聚合框架来执行这些更复杂的查询.>

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?

解决方案

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.

这篇关于如何通过 Java Driver 从 MongoDB 中的不同列表中排序结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆