Monday, July 9, 2012

MongoDB: Save Raw JSON With Java API

In MongoDB's Java API, most of the functions relating to saving objects into the DB requires you to either provide a map or add each property one by one. In situations where you already have the JSON document (e.g. submitted via Javascript), you'd want to just directly save the JSON document.

To do this in MongoDB:
DBObject dbObject = (DBObject) JSON.parse(jsonStr);
Where the "JSON" class is "com.mongodb.util.JSON".

Reference: http://www.mkyong.com/mongodb/java-mongodb-convert-json-data-to-dbobject/

No comments:

Post a Comment