How to insert the embedded documents using BSONObject
? When I am trying to insert the embedded document, getting error shown below.
I wanted to save null values for the Date and firstName for example. I tried some options using Spring Data Mongo, but it doesn't work out well.
I am getting below error:
java.lang.IllegalStateException: Failed to execute CommandLineRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE] at com.example.MongoPocApplication.main(MongoPocApplication.java:24) [classes/:na] Caused by: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.example.Hobbies. at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46) ~[bson-3.8.2.jar:na] at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63) ~[bson-3.8.2.jar:na] at org.bson.codecs.configuration.ChildCodecRegistry.get(ChildCodecRegistry.java:51) ~[bson-3.8.2.jar:na] at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:231) ~[mongodb-driver-core-3.8.2.jar:na] at com.mongodb.DBObjectCodec.encodeIterable(DBObjectCodec.java:292) ~[mongodb-driver-core-3.8.2.jar:na] at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:219) ~[mongodb-driver-core-3.8.2.jar:na] at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:149) ~[mongodb-driver-core-3.8.2.jar:na] at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:65) ~[mongodb-driver-core-3.8.2.jar:na] at com.mongodb.BasicDBObject.toJson(BasicDBObject.java:194) ~[mongodb-driver-core-3.8.2.jar:na] at com.mongodb.BasicDBObject.toJson(BasicDBObject.java:167) ~[mongodb-driver-core-3.8.2.jar:na] at com.mongodb.BasicDBObject.toJson(BasicDBObject.java:154) ~[mongodb-driver-core-3.8.2.jar:na] at com.mongodb.BasicDBObject.toString(BasicDBObject.java:238) ~[mongodb-driver-core-3.8.2.jar:na] at java.lang.String.valueOf(String.java:2994) ~[na:1.8.0_171] at java.io.PrintStream.println(PrintStream.java:821) ~[na:1.8.0_171] at com.example.MongoPocApplication.savePersons(MongoPocApplication.java:70) [classes/:na] at com.example.MongoPocApplication.run(MongoPocApplication.java:34) [classes/:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
Person.java
@Builder @Data @AllArgsConstructor @NoArgsConstructor @Document public class Person { @Id private String id; @Field private String firstName; @Field private String lastName; @Field private String emailId; @Field private List<Hobbies> hobbies; }
Hobbies:
@Builder @Data @AllArgsConstructor @NoArgsConstructor public class Hobbies { private String interest; private String sports; }
Main
BSONObject personBsonObj = BasicDBObjectBuilder.start() .add("firstName","John") .add("lastName", null) .add("email","john.kerr@gmail.com") .add("hobbies",Arrays.asList(hobbies1, hobbies2, hobbies4)).get(); BSONObject insert = mongoTemplate.insert(personBsonObj,"person"); System.out.println(insert);