import org.mongodb.morphia.Datastore; //导入方法依赖的package包/类
public static void main(final String[] args) throws UnknownHostException {
final Morphia morphia = new Morphia();
// tell morphia where to find your classes
// can be called multiple times with different packages or classes
morphia.mapPackage("org.mongodb.morphia.example");
// create the Datastore connecting to the database running on the default port on the local host
final Datastore datastore = morphia.createDatastore(new MongoClient(), "morphia_example");
datastore.getDB().dropDatabase();
datastore.ensureIndexes();
final Employee elmer = new Employee("Elmer Fudd", 50000.0);
datastore.save(elmer);
final Employee daffy = new Employee("Daffy Duck", 40000.0);
datastore.save(daffy);
final Employee pepe = new Employee("Pepé Le Pew", 25000.0);
datastore.save(pepe);
elmer.getDirectReports().add(daffy);
elmer.getDirectReports().add(pepe);
datastore.save(elmer);
Query query = datastore.find(Employee.class);
final List employees = query.asList();
Assert.assertEquals(3, employees.size());
List underpaid = datastore.find(Employee.class)
.filter("salary <=", 30000)
.asList();
Assert.assertEquals(1, underpaid.size());
underpaid = datastore.find(Employee.class)
.field("salary").lessThanOrEq(30000)
.asList();
Assert.assertEquals(1, underpaid.size());
final Query underPaidQuery = datastore.find(Employee.class)
.filter("salary <=", 30000);
final UpdateOperations updateOperations = datastore.createUpdateOperations(Employee.class)
.inc("salary", 10000);
final UpdateResults results = datastore.update(underPaidQuery, updateOperations);
Assert.assertEquals(1, results.getUpdatedCount());
final Query overPaidQuery = datastore.find(Employee.class)
.filter("salary >", 100000);
datastore.delete(overPaidQuery);
}