mongodb java driver 3.0: how to store JSON document

Seweryn Niemiec picture Seweryn Niemiec · Apr 14, 2015 · Viewed 10.8k times · Source

Looks basic and simple: A have a JSON string and I want to store it in MongoDB as JSON document.

In java driver 2.xx I could use com.mongodb.util.JSON.parse(String jsonString) to get DBObject and then store it in collection.

In driver 3.0 JSON.parse still gives DBObject, but rest of API uses org.bson.Document class which looks as incompatible with DBObject.

How to do it in driver version 3.0?

Answer

Ross picture Ross · Apr 15, 2015

For Document use the parse() static helper:

Document myDoc = Document.parse(jsonString)