日期:2014-05-16 浏览次数:20480 次
@Entity(value="name",noClassnameStored=true) public class MyEntity { @Id public ObjectId id; public String name; }
public static void main(String[] args) throws Exception{ Morphia morphia = new Morphia(); Mongo mongo = new Mongo("127.0.0.1",27017); Datastore ds = morphia.createDatastore(mongo, "mydb"); // new Morphia().createDatastore("mydb"); ds.ensureIndexes(); ds.ensureCaps(); MyEntity me = new MyEntity(); me.name = "hsys"; ds.save(me); MyEntity e3 = ds.find(MyEntity.class).get(); UpdateOperations<MyEntity> ops = ds.createUpdateOperations(MyEntity.class).set("name", "淮少"); //UpdateOperations<MyEntity> ops = ds.createUpdateOperations(MyEntity.class).unset("name"); ds.update(ds.createQuery(MyEntity.class).field(Mapper.ID_KEY).equal(e3.id), ops); for(MyEntity me2 : ds.find(MyEntity.class)){ System.out.println(me2.name); } }