简体   繁体   中英

Spring How to insert a raw JSON directly into mongo db without Parsing

I want to insert the JSON string directly into mongo db, without parsing

String json = "{ 'name' : 'Vince' , " +
                "'website' : 'howtodoinjava.com' , " +
                "'address' : { 'addressLine1' : 'Some address' , " +
                              "'addressLine2' : 'Cardiff CF116LW' , " 
                              "'addressLine3' : 'Cardiff, UK'} }"

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

collection.insert(dbObject);

In the above example i don't want to use this line

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

Is there any out of the box solution for the same ?

The insert method takes one or more DBObject as parameters, so no you cannot pass a String instead.

There are a few ways to obtain a DBObject ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM