Is there a simple way to convert Simple POJO to org.bson.Document?
I'm aware that there are ways to do this like this one:
Document doc = new Document();
doc.append("name", person.getName()):
But does it have a much simpler and typo less way?
Is there a simple way to convert Simple POJO to org.bson.Document?
I'm aware that there are ways to do this like this one:
Document doc = new Document();
doc.append("name", person.getName()):
But does it have a much simpler and typo less way?
You can use
Gson
andDocument.parse(String json)
to convert a POJO to aDocument
. This works with the version 3.4.2 of java driver.Something like this:
Currently Mongo Java Driver 3.9.1 provide POJO support out of the box
http://mongodb.github.io/mongo-java-driver/3.9/driver/getting-started/quick-start-pojo/
Let's say you have such example collection with one nested object
1. Provide a MongoDatabase bean with proper CodecRegistry
2. Annotate your POJOS
3. Query mongoDB and obtain POJOS
And that's it !!! You can easily obtain your POJOS without cumbersome manual mappings and without loosing ability to run native mongo queries
The point is, that you do not need to put your hands on org.bson.Document.
Morphia will do all that for you behind the curtain.
Here you will find several examples: https://mongodb.github.io/morphia/