Datastore
get
根据@id返回一个实例,find()的简洁使用,查找不到结果时,返回null
Hotel hotel = ds.get(Hotel.class, hotelId);
Find
对Query的包装,可以返回Query,也支持Iterable<T> and the QueryResults interface。
支持操作:["=", "==","!=", "<>", ">", "<", ">=", "<=", "in", "nin", "all", "size", "exists"]
Datastore ds = ...
//use in a loop
for(Hotel hotel : ds.find(Hotel.class, "stars >", 3))
print(hotel);
//get back as a list
List<Hotel> hotels = ds.find(Hotel.class, "stars >", 3).asList();
//sort the results
List<Hotel> hotels = ds.find(Hotel.class, "stars >", 3).sort("-stars").asList();
//get the first matching hotel, by querying with a limit(1)
Hotel gsHotel = ds.find(Hotel.class, "name", "Grand Sierra").get();
//same as
Hotel gsHotel = ds.find(Hotel.class, "name =", "Grand Sierra").get();
Save
Hotel hotel = new Hotel();
ds.save(hotel);
//@Id field is filled in for you (after the save), if you didn't set it.
ObjectId id = hotel.getId();
Delete
方法将删除对象的基于查询的id或Entity。
Datastore ds = ...
ds.delete(Hotel.class, "Grand Sierra Resort");
//use a query
ds.delete(ds.createQuery(Hotel.class).filter("pendingDelete", true));
FindAndDelete
删除一个对象,同时返回删除的对象,该方法会查找到第一个对象并且删除
Hotel grandSierra = ds.findAndDelete(ds.get(Hotel.class, "Grand Sierra Resort"));
Update
更新用户最后一次登录时间
public void loggedIn()
{
long now = System.currentTimeMillis();
Query<User> me = datastore.createQuery(User.class).field(Mapper.ID_KEY).equal(id);
UpdateOperations<User> ops = datastore.createUpdateOperations(User.class).set("lastLogin", now);
ds.update(me , ops);
lastLogin = now;
}
ensure index and caps
在注册实例到morphia之后,每次启动应用、管理实例或部署脚本的时候,创建index和包含的集合, 同步的
如果系统已经存在index和包含的集合,那将不做任何操作,如果当前设置不一样,将报错写日志,但不对已存在的集合做任何操作
m.map(MyEntity.class);
ds.ensureIndexes(); //creates all defined with @Indexed
ds.ensureCaps(); //creates all collections for @Entity(cap=@CappedAt(...))
Updating(On the server)总之,当你通过Datesote调用update方法时就会向MongoDB服务器发送一个修改已存在数据的指令。
- interface Datastore {
- ...
- /** updates all entities found with the operations*/
- <T> UpdateResults<T> update(Query<T> query, UpdateOperations<T> ops);
- /** updates all entities found with the operations; if nothing is found insert the update as an entity if "createIfMissing" is true*/
- <T> UpdateResults<T> update(Query<T> query, UpdateOperations<T> ops, boolean createIfMissing);
- /** updates the first entity found with the operations*/
- <T> UpdateResults<T> updateFirst(Query<T> query, UpdateOperations<T> ops);
- /** updates the first entity found with the operations; if nothing is found insert the update as an entity if "createIfMissing" is true*/
- <T> UpdateResults<T> updateFirst(Query<T> query, UpdateOperations<T> ops, boolean createIfMissing);
- /** updates the first entity found with the operations; if nothing is found insert the update as an entity if "createIfMissing" is true*/
- <T> UpdateResults<T> updateFirst(Query<T> query, T entity, boolean createIfMissing);
- }
- public interface UpdateOperations<T> {
- /** sets the field value */
- UpdateOperations<T> set(String fieldExpr, Object value);
- /** removes the field */
- UpdateOperations<T> unset(String fieldExpr);
- /** adds the value to an array field*/
- UpdateOperations<T> add(String fieldExpr, Object value);
- UpdateOperations<T> add(String fieldExpr, Object value, boolean addDups);
- /** adds the values to an array field*/
- UpdateOperations<T> addAll(String fieldExpr, List<?> values, boolean addDups);
- /** removes the first value from the array*/
- UpdateOperations<T> removeFirst(String fieldExpr);
- /** removes the last value from the array*/
- UpdateOperations<T> removeLast(String fieldExpr);
- /** removes the value from the array field*/
- UpdateOperations<T> removeAll(String fieldExpr, Object value);
- /** removes the values from the array field*/
- UpdateOperations<T> removeAll(String fieldExpr, List<?> values);
- /** decrements the numeric field by 1*/
- UpdateOperations<T> dec(String fieldExpr);
- /** increments the numeric field by 1*/
- UpdateOperations<T> inc(String fieldExpr);
- /** increments the numeric field by value (negatives are allowed)*/
- UpdateOperations<T> inc(String fieldExpr, Number value);
- }
The Field Expression属性表达式是用在所有的操作上的,可以是单个的属性名,也可以是用点“.”连接的嵌套属性。在表达式中你也可以使用位置操作副($)。在属性表达式中没有标准,你可以使用任何在MongoDB服务端有效符号。事例初始化一下所有的实例创建的连接和Morphia实例都使用的一下代码。
- Morphia morphia = new Morphia();
- morphia.map(Hotel.class).map(Address.class);
- Datastore datastore = morphia.createDatastore("MorphiaSampleDb");
- Hotel hotel = new Hotel("Fairmont", 3, new Address("1 Rideau Street", "Ottawa", "K1N8S7", "Canada"));
- datastore.save(hotel);
- UpdateOperations<Hotel> ops;
- // This query will be used in the samples to restrict the update operations to only the hotel we just created.