注意 使用mongo-java-driver 使用3.8.0版本
FindIterable<Document> limit = obj1.find(query).sort(new BasicDBObject(“barTime”, -1)).limit(1);
MongoCursor<Document> iterator = limit.iterator(); // 遍历所有的document
while (iterator.hasNext()) {
// https://stackoverflow.com/questions/35209839/converting-document-objects-in-mongodb-3-to-pojos
// 修正转换格式
JsonWriterSettings settings =
JsonWriterSettings.builder().int64Converter(new Converter<Long>() {
public void convert(Long value, StrictJsonWriter writer) {
writer.writeNumber(value.toString());
}
}).build();
String s = iterator.next().toJson(settings); // 将整数int转换为字符串String
Gson g = new Gson();
KbarPO kLine = g.fromJson(s, KbarPO.class);
System.out.println(kLine);
}
转载于:https://www.cnblogs.com/redBack/p/9562280.html