Inserting Java Object to MongoDB Collection Using Java

后端 未结 11 1184
情话喂你
情话喂你 2020-12-28 13:34

I am trying to insert a whole Java object into a MongoDB Collection using Java. I am getting following error:

Error :

Exception in t         


        
11条回答
  •  一整个雨季
    2020-12-28 13:48

    You can convert your java object into json string using the gson library and then insert it in mongodb.

    Eg:

    Gson gson = new Gson();
    String json = gson.toJson(Employee);    
    BasicDBObject basicDBObject = new BasicDBObject("Name", json );          
    DBCollection dbCollection = db.getCollection("NameColl");          
    dbCollection.save(basicDBObject);    
    

提交回复
热议问题