Converting DBCursor to JSON

1,793 views
Skip to first unread message

Abhishek kumar

unread,
Sep 6, 2011, 2:46:01 PM9/6/11
to mongodb-user
I am doing following in my application server,

DBCollection collection1 = mongoDB.getCollection("messages");
DBCursor dbCursor1 = collection1.find().limit(5);

I have to send a JSON object to the client from the server, so how can
I convert DBCursor Object to JSON

Mathias Stearn

unread,
Sep 6, 2011, 3:17:27 PM9/6/11
to mongod...@googlegroups.com
I think you want to iterate the cursor and convert each returned object to JSON then put them into an array.

Abhishek kumar

unread,
Sep 6, 2011, 11:08:32 PM9/6/11
to mongodb-user
How can I convert DBCursor Object to JSON, is there any direct method/
function
also
is there any single function which directly convert DBCursor to JSON

Abhishek kumar

unread,
Sep 6, 2011, 11:14:04 PM9/6/11
to mongodb-user
What I am trying to achieve is, I dont want extra overhead at the
server side, I just want to retrieve the results from the mongodb
database and send to client as JSON. As mongodb stores in form of
BSON, so can there be any way, so that I dont require extra overhead
at the server side to convert to JSON and then send to the client

Scott Hernandez

unread,
Sep 6, 2011, 11:15:54 PM9/6/11
to mongod...@googlegroups.com
You should be able to call com.mongodb.util.JSON.serialize(...) and
pass in the cursor. It will return a json string which is an array and
includes all the docs in that json string.

BSON != JSON, so there will have to be a bit of decoding/encoding
going on in java.

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages