I have a json document that has nested array elements. But when I add the document to my mongo db I only writes the last array using the following code:
mongoColl = mongoDatabase.getCollection("coll");
DBObject dbo = (DBObject)JSON.parse(myJsonString);
mongoColl.insert(dbo);
dbObject = mongoColl.findOne();
String s = JSON.serialize(dbObject);
JSONObject json = null;
try
{
json = new JSONObject(s);
System.out.println(json.toString(4));
}
catch (JSONException e1)
{
e1.printStackTrace();
}
I puzzled as there is no exception, who do I correctly insert all the array elements in my json document?
Edit I have tried several methods and all output the same result (the last array element in my json document)
Here is my json document: My JSON doc
// Method 2
Object jsonObj = s;
Object o = com.mongodb.util.JSON.parse(jsonObj.toString());
DBObject dbObj = (DBObject) o;
WriteResult result = mongoColl.insert(dbObj);
System.out.println("Write Result: " + result);
// Method 3
JSONArray jsonArray = new JSONArray(s);
JSONObject jsonObject = new JSONObject();
jsonObject.put("GameofThrones", jsonArray);
DBObject bson = (DBObject) JSON.parse(jsonObject.toString());
WriteResult result = mongoColl.insert(bson);
System.out.println("Write Result: " + result);
Here is the result: Result
