After going through tons of examples, I couldn't find relevant code, Basically I want to serialize the JSON array over the wire to the client.
MongoCursor<Document> cursor = ((MongoCollection)data).find().iterator();
try {
while (cursor.hasNext()) {
response.getWriter().write(cursor.next().toJson());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
cursor.close();
}
break;
Attempt Doesn't work but is something close to what I'm looking for.
MongoCursor<Document> cursor = ((MongoCollection)data).find().iterator();
JsonWriter jsonWriter = new JsonWriter(response.getWriter());
jsonWriter.writeStartDocument();
jsonWriter.writeStartArray();
while(cursor.hasNext()) {
jsonWriter.writeStartDocument();
jsonWriter.writeString(cursor.next().toJson());
jsonWriter.writeEndDocument();
}
jsonWriter.writeEndArray();
jsonWriter.writeEndDocument();
cursor.close();
Any serialization solutions for mongodb collections?
Naive way works
try {
response.getWriter().write("[");
while (cursor.hasNext()) {
response.getWriter().write(cursor.next().toJson() + (cursor.hasNext() ? "," : ""));
}
response.getWriter().write("]");
} finally {
cursor.close();
}