I'm using the below sample json :
JSONObject json=new JSONObject();
json.put("time_range", "22-23");
json.put("flow_id", "786");
And trying to convert to Document as follows :
Document doc = (Document) JSON.parse(jsonlist.toString()); // conversion from json to Document
col.insertOne(doc); // inserting into Mongo collection
I'm facing the below error:
java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to org.bson.Document
Can anyone please help me out regarding this issue ...
Does MongoDB use BSON or JSON? MongoDB stores data in BSON format both internally, and over the network, but that doesn't mean you can't think of MongoDB as a JSON database. Anything you can represent in JSON can be natively stored in MongoDB, and retrieved just as easily in JSON.
Open the Import Wizard. Then, choose JSON as the import format and click OK. Click on + to add JSON source documents, – to remove them, or the clipboard icon to paste JSON data from the clipboard.
Converting a BSON Object to a JSON StringUse the asString method to convert the BSON document to an extended JSON string. See http://docs.mongodb.org/manual/reference/mongodb-extended-json/ for more information on extended JSON.
JSON is a data format that represents the values of objects, arrays, numbers, strings, booleans, and nulls. The Extended JSON format defines a reserved set of keys prefixed with " $ " to represent field type information that directly corresponds to each type in BSON, the format that MongoDB uses to store data.
Try this
Document doc = Document.parse( jsonlist.toString() );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With