我查询我的数据库以获取两个日期之间的历史值,我想获得一个json字符串而不是FindIterable对象。
现在我这样做:
public static MongoResponse getHistory(MongoRequest mongoRequest) {
MongoResponse result = new MongoResponse();
MongoDatabase db = client.getDatabase(mongoRequest.getDbName());
MongoCollection coll = db.getCollection(mongoRequest.getCollName());
FindIterable iterable = coll.find(new Document("date", new Document("$gt", mongoRequest.getFromDate())
.append("$lte", mongoRequest.getToDate()))).sort(Sorts.ascending("date"));
if (!iterable.iterator().hasNext()) {
result.setType(MongoResponse.Type.ERROR);
} else {
result.setType(MongoResponse.Type.RESPONSE);
iterable.forEach(new Block<Document>() {
@Override
public void apply(Document t) {
result.addContent(t.toJson());
}
});
}
return result;
}