0
我已經連接了Mongo數據庫與Java和我已經有數據在數據庫中。數據庫中的數據如下所示:如何用java驅動程序對mongodb中的整數進行排序?
{"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中,我想按「錢」對數據進行排序。 ,所以我寫這樣的代碼:
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
....
它以某種方式排序,但那不是我想要的。 也許有另一種方法來排序數據,我想要它。
您的貨幣字段存儲爲字符串。所以它被正確地排序爲一個字符串。如果你想按數字順序排序,你需要轉換爲數字。 –
它的工作原理!非常感謝你的幫助 – user2843943