【发布时间】:2017-03-05 15:19:42
【问题描述】:
我有一个DBList,其中包含来自cursor = coll.find() 查询的DBObjects。我在每个DBObject 中有一个名为timestamp 的字段,我想按 最新时间戳 对DBList 进行排序。我知道我可以在这里使用Collections.sort(),但是对于 mongoDB 的 java API 来说是新手,我似乎无法根据特定字段进行排序。我非常感谢您的帮助。
unsorted list 是这样形成的:
DBCursor cursor = coll.find(whereQuery);
while(cursor.hasNext()) {
DBObject o = cursor.next();
System.out.println(o.toString());
unsortedList.add(o);
}
在此之后,我想按timestamp 的降序对unsortedList 进行排序。我不太清楚如何做到这一点,并提前感谢您的帮助。
也不能使用诸如coll.find().sort(timestamp,-1) 之类的解决方案,因为有一个外部for 循环会不断更改whereQuery。 list 必须仅在查询完成后 进行排序 。
【问题讨论】:
标签: java mongodb sorting collections