Currently I’m using MongoDB on a project and I’m accessing it through the Java driver 3.x.

The problem that I had, and some other people on stackoverflow but there were no answers, comes when you try to retrieve a document that you inserted in the JSON format, it returns it in JSON extended format or in JSON but in an altered structure.

Using this piece of code I get a wrong result:

Document document;
String json = document.toString();

I’m get this and this isn’t what I inserted in the database.

"source_status_id" : { "$numberLong" : "68578585501732352" }

This is what I inserted and want:

"source_status_id" : 68578585501732352

The way to overpass this problem is to create a JSONObject from an other library ie twitter4j and then use the toString() method from that object…

Document document = document;
String json = new twitter4j.JSONObject(document).toString();

Which works as a charm and returns it in the original JSON format! 😀

Advertisements