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
I resolve it by adding the spring-boot-starter-data-mongodb dependency in your pom.xml or your build.gradle.
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-mongodb', version: '2.2.4.RELEASE'
Documentation: MongoDB Driver Quick Start - POJOs
After following the above document, if you are still getting error, then
you could be using a generic document inside your collection like
class DocStore {
String docId:
String docType;
Object document; // this will cause the BSON cast to throw a codec error
Map<String, Object> document; // this won't
}
And still, you would want to cast your document from POJO to Map
mkyong tutorial could help.
As for the fetch, it works as expected but you might want to cast from Map to your POJO as a post-processing step, we can find some good answers here
Hope it helps!