MongoDB Java Inserting Throws org.bson.codecs.configuration.CodecConfigurationException: Cannot find codec for class io.github.ilkgunel.mongodb.Pojo

I am learning MongoDB with Java. I am trying to insert data into MongoDB using a Java driver. I am doing the insertion, as in the MongoDB tutorial, and all this is okey. But if I want to insert a variable, and when I run the code, the driver will give this error:

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class io.github.ilkgunel.mongodb.Pojo. 

I searched for questions in Stack Overflow like this, but I couldn’t understand anything, and I can’t find anything to solve this error. My code is below. How to solve this problem?

I am using this code:

 package io.github.ilkgunel.mongodb; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Locale; public class MongoDBBasicUsage { public static void main(String[] args) { MongoClient mongoClient; try { Pojo pojo = new Pojo(); mongoClient = new MongoClient("localhost", 27017); MongoDatabase database = mongoClient.getDatabase("MongoDB"); pojo.setId("1"); pojo.setName("ilkay"); pojo.setSurname("günel"); Document document = new Document(); document.put("person", pojo); database.getCollection("Records").insertOne(document); } catch (Exception e) { System.err.println("Bir Hata Meydana Geldi!"); System.out.println("Hata" + e); } } } 

My pojo:

  package io.github.ilkgunel.mongodb; public class Pojo { String name; String surname; String id; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public String getId() { return id; } public void setId(String id) { this.id = id; } } 
+6
source share
2 answers

In terms of what you are trying to do, you are trying to add some kind of custom data type (in this case your POJO), but what you need to keep in mind is that the fields in the documents can only accept certain data types , not directly.

If you did not already know, Mongo Documents are structured in the same way as json. Therefore, you must explicitly create documents by creating fields and inserting values ​​into them. There are certain data types that are allowed in value fields:

http://mongodb.imtqy.com/mongo-java-driver/3.0/bson/documents/

To help in your case, the code below takes POJO as a parameter and knowing the structure of the POJO, returns a Mongo Document that can be inserted into your collection:

 private Document pojoToDoc(Pojo pojo){ Document doc = new Document(); doc.put("Name",pojo.getName()); doc.put("Surname",pojo.getSurname()); doc.put("id",pojo.getId()); return doc; } 

This should work for insertion. If you want to index one of the fields:

 database.getCollection("Records").createIndex(new Document("id", 1)); 

I hope this answers your question and works for you.

+9
source

You checked the mongodb library. I solve this problem this morning by changing the java mongodb driver from 3.2.2 to 3.4.2. new maven like this:

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> <version>1.5.4.RELEASE</version> </dependency> 

there is an attempt and answer

0
source

All Articles