13

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 ...

dev777
  • 999
  • 6
  • 17
  • 34

2 Answers2

28

Try this

Document doc = Document.parse( jsonlist.toString() );
Young Emil
  • 2,220
  • 2
  • 26
  • 37
0

You can try inserting data in mongo using below sample code :-

String json = "{ 'name' : 'lokesh' , " +
                "'website' : 'howtodoinjava.com' , " +
                "'address' : { 'addressLine1' : 'Some address' , " +
                              "'addressLine2' : 'Karol Bagh' , " +
                              "'addressLine3' : 'New Delhi, India'}" +
                            "}";

DBObject dbObject = (DBObject)JSON.parse(json);

collection.insert(dbObject);

For your case you can Stringify your JSON object and then try inserting it into mongodb..

anujjain0801
  • 122
  • 3