POJO to org.bson.Document and Vice Versa

yesterdaysfoe :

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?

midnight :

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.

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 will find several examples: https://mongodb.github.io/morphia/

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=453966&siteId=1