I\'m learning MongoDB with Java. I\'m trying to insert data to MongoDB with Java driver. I\'m doing inserting like in MongoDB tutorial and every thing is okey. But if I want
You need to configure the CodeRegistry to use the PojoCodecProvider as explained here: http://mongodb.github.io/mongo-java-driver/3.7/driver/getting-started/quick-start-pojo/
From the looks of what you are trying to do, you are trying to add some custom data type (in this case your POJO) but what you need to keep in mind is that fields in documents can only accept certain data types, not objects directly.
In case if you didn't know also, Mongo Documents are structured the same way as json. So you have to explicitaly create the documents by creating the fields and inserting the values into them. There are specific data types that are allowed in value fields:
http://mongodb.github.io/mongo-java-driver/3.0/bson/documents/
To help with your case, the code below takes your 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.
did you check the library of mongodb. I solve this problem in this morning by change the mongodb java driver from 3.2.2 to 3.4.2. the new maven like that:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
have a try and response
To be bit abstract, as this may save the day for other developers..
This error: CodecConfigurationException: Can't find a codec for class xxx means that your mongo driver is not able to handle the data you sent in the object you made of that xxx class and accordingly can't generate the mongo query you want.
The resolution in that case would be either to use the right class i.e to use one of the expected classes by the driver (in my case replacing java array by ArrayList object resolved the issue).. The other resolution could be to upgrade your driver. Third solution could be as mentioned by @Renato, to define your own decoding logic.. This depends on your exact case.
hth
Use below set of lines while making connection to mongo db for resolving the issue with codec, while using POJO class in mongo.
Reference : https://developer.mongodb.com/quickstart/java-mapping-pojos
ConnectionString connectionString = new ConnectionString("mongodb://" + username + ":" + password + "@" + host + ":" + port);
CodecRegistry pojoCodecRegistry = fromProviders(PojoCodecProvider.builder().automatic(true).build());
CodecRegistry codecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), pojoCodecRegistry);
MongoClientSettings clientSettings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.codecRegistry(codecRegistry)
.build();
MongoClient mongoClient = MongoClients.create(clientSettings);
MongoDatabase mongoDatabase = mongoClient.getDatabase(database);
I'm using MongoDB POJO support. In my case, I had this "clue" WARNing before the exception:
org.bson.codecs.pojo : Cannot use 'UserBeta' with the PojoCodec.
org.bson.codecs.configuration.CodecConfigurationException: Property 'premium' in UserBeta, has differing data types: TypeData{type=PremiumStatus} and TypeData{type=Boolean}.
My class UserBeta
had getPremium()
, setPremium()
, and isPremium()
. I already @BsonIgnore
-d the isPremium()
but for some reason it's still detected and causing conflict.
My workaround is to rename isPremium()
to isAnyPremium()
.