我已经将mongo数据库与java连接,我已经拥有数据库中的数据。数据库中的数据如下所示:
{"id":"4654654", "money":"54"}
{"id":"44234654", "money":"102"}
{"id":"123654", "money":"36"}
{"id":"98764654", "money":"5400"}
{"id":"456655", "money":"520"}
{"id":"7654", "money":"789"}
{"id":"3456546", "money":"85"}
{"id":"0346575", "money":"42"}
{"id":"46554645", "money":"2000"}
在java中我想用" money"对数据进行排序。 所以我写这段代码:
DBCursor cursor = collection.find().sort(new BasicDBOject("money", 1));
while(cursor.hasNext(){
System.out.println(cursor.next().get("money"));
}
我希望我会得到这样的东西:
36
42
54
85
102
......
但这不是我得到的,我得到这些数字:
102
2000
36
42
520
5400
....
它以某种方式排序,但这不是我想要的。 也许还有另一种方法可以对数据进行排序。
答案 0 :(得分:0)
添加此作为答案(从我的评论转移),因为它似乎现在已经解决了问题。你能否将这个问题标记为已解决?
您的money字段存储为字符串。因此它被正确地排序为字符串。如果您希望以数字方式对其进行排序,则需要转换为数字。