BasicDBObject whereQuery = new BasicDBObject();
DBCollection coll = db.getCollection("mycollection"); whereQuery.put("employee", "bonus");
DBCursor cursor =coll.find(whereQuery);
cursor.pretty();
上下文我无法执行cursor.pretty(),因为方法与DBCursor
无关
我正在使用
打印数据 while(cursor.hasNext())
{
out.println(cursor.next(););
out.println("</br>");
}
请给我一个在servlet中进行漂亮打印的方法
答案 0 :(得分:0)
据我所知,漂亮的功能仅在mongo shell中可用。你必须自己编写一个函数。
答案 1 :(得分:0)
我无法在DBCursor
中找到漂亮的东西。通过使用基于Google Gson的the tutorial,我发现了一项解决方案,如下所述
Document myDoc = collection.find().first();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(myDoc);
System.out.println(json);
将打印如下,我有mongodb java驱动程序3.2.1
{
"_id": {
"$oid" : "50906d7fa3c412bb040eb577"
},
"student_id": 0,
"type": "exam",
"score": 54.6535436362647
}
以下代码可能
DBObject myObj = cursor.hasNext();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(myObj);
out.println(json);
out.println("<br>");
答案 2 :(得分:0)
while (cursorDoc.hasNext()) {
System.out.println("Printning value of cursor " +cursorDoc.next());
if(cursorDoc.hasNext()) {
DBObject o = cursorDoc.next();
String Name= (String) o.get("Name") ;
request.setAttribute("shopname",shopname);
RequestDispatcher dispatcher =request.getRequestDispatcher(page);
dispatcher.forward(request, response);
}