简体   繁体   中英

[JAVA]: How to insert and retrieve Dynamic JSON in MongoDB

I am developing JAX-RS web service with MongoDB in backend. I am using Morphia for Class to Mongo Collection Mapping. In one of the insert operation we have a Json inside a JSON.

{
    "deviceId" : "57b5afc7e91c14ce01425fb6",
    "data":{
        "mah":50,
        "volt":23
    },
    "isSynced" : true
}

In this the JSON with key "data" is dynamic. which can be anything.

How should I insert a dynamic JSON inside collection? As it is dynamic I cannot map it to a class.

You could store that JSON as a Map inside your entity. The generics might get funky, though, since the value type would likely have to be Object .

This is resolved. I used com.mongodb.BasicDBObject for this. This has in-built constructor which serves the purpose.

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