Java – POJO to org bson. Document and vice versa
•
Java
Is there a simple way to convert simple POJOs to org. POJOs bson. Document?
I know there are many ways to do this:
Document doc = new Document(); doc.append("name",person.getName()):
But does it have a simpler and more wrong way?
Solution
The point is, you don't need to put your hand on org bson. On document
Morphia will do all this for you behind the scenes
import com.mongodb.MongoClient; import org.mongodb.morphia.Datastore; import org.mongodb.morphia.DatastoreImpl; import org.mongodb.morphia.Morphia; import java.net.UnkNownHostException; ..... private Datastore createDataStore() throws UnkNownHostException { MongoClient client = new MongoClient("localhost",27017); // create morphia and map classes Morphia morphia = new Morphia(); morphia.map(FooBar.class); return new DatastoreImpl(morphia,client,"testmongo"); } ...... //with the Datastore from above you can save any mapped class to mongo Datastore datastore; final FooBar fb = new FooBar("hello","world"); datastore.save(fb);
Here you can find several examples: https://mongodb.github.io/morphia/
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码