1

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);
  • unfortunately I am not able to replicate the issue. May I know the spring boot version and mongo driver version used? – Barath Apr 10 '19 at 05:23
  • Take a look here [Codec issue](https://stackoverflow.com/questions/39433775/mongodb-java-inserting-throws-org-bson-codecs-configuration-codecconfigurationex) . I tried with mongo 3.8.x driver and it went through – Barath Apr 10 '19 at 05:27
  • Spring Boot version 2.1.3.RELEASE and I keep whatever Spring MongoDB starter is pulling as dependent jards –  Apr 10 '19 at 05:27
  • Your code doesn't have embedded documents. Will you please try with the same as mine ? –  Apr 10 '19 at 05:28
  • here is the sample link [issue-55604660](https://github.com/BarathArivazhagan/stackoverflow-issues/tree/master/issue-55604660-mongo-codec-registry) – Barath Apr 10 '19 at 05:38
  • Below link may be helpful as I feel the topic is related https://stackoverflow.com/a/60541155/9832322 – Abhilash Mar 05 '20 at 10:08

2 Answers2

1

I found the same problem in one of my classes. The problem is that you declared more than a constructor for your bean. Don't know why, and how to relate with the message of the exception.

@AllArgsConstructor
@NoArgsConstructor

The problem here is not Lombok, but the fact that Lombok inserts into your class two constructors:

public Person(String id, 
              String firstName, 
              String lastName, 
              String emailId,
              List<Hobbies> hobbies) { /* ... */ }

public Person() { /* ... */ }

Delete one of the two constructors, and the problem with the codec will go away.

I hope it helps.

riccardo.cardin
  • 7,971
  • 5
  • 57
  • 106
0

I was able to solve this issue like below. I wonder if we can do this using Spring Data Mongo Repository query ?

Document hobbies1 = new Document();
hobbies1.put("interest", "Indoor");
hobbies1.put("sports", "Chess");

Document hobbies2 = new Document();
hobbies2.put("Loveoor", "Table Tennis");
hobbies2.put("Gamedoor", "Cricket");

BSONObject personBsonObj = BasicDBObjectBuilder.start()
    .add("firstName","John")
    .add("lastName", null)
    .add("email","john.kerr@gmail.com")
    .add("hobbies",Arrays.asList(hobbies1, hobbies2)).get();
BSONObject insert = mongoTemplate.insert(personBsonObj,"person");
    System.out.println(insert);
  • Please share the code to reproduce the issue. Did you construct MongoTemplate ? – Barath Apr 10 '19 at 05:56
  • @Barath - Sorry for the latest response. I've updated post and uploaded code https://github.com/JavaHelper/PiotrsTechBlogExample1/tree/master/mongo-poc –  Apr 10 '19 at 06:24