有没有简单的方法将Simple POJO转换为org.bson.Document?
我知道有很多方法可以像这样做:
Document doc = new Document(); doc.append("name",person.getName()):
解决方法
关键是,你不需要把手放在org.bson.Document上.
Morphia将在幕后为你做所有这些.
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);
在这里您可以找到几个例子:https://mongodb.github.io/morphia/