Morphia 学习二 接口

本文介绍了Morphia接口中关于Datastore的使用,包括根据ID获取实例、Geo-spatial查询以及如何进行结果限制和排序。通过示例展示了如何使用filter和limit方法来限定查询结果数目,并指出skip方法的效率问题。此外,还提及了MongoDB支持选择性返回实体对象的部分字段。
摘要由CSDN通过智能技术生成

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服务器发送一个修改已存在数据的指令。                 
[java]  view plain copy
  1.  interface Datastore {  
  2.      ...  
  3.     
  4.   
  5.         /** updates all entities found with the operations*/  
  6.         <T> UpdateResults<T> update(Query<T> query, UpdateOperations<T> ops);  
  7.         /** updates all entities found with the operations; if nothing is found insert the update as an entity if "createIfMissing" is true*/  
  8.         <T> UpdateResults<T> update(Query<T> query, UpdateOperations<T> ops, boolean createIfMissing);  
  9.         /** updates the first entity found with the operations*/  
  10.         <T> UpdateResults<T> updateFirst(Query<T> query, UpdateOperations<T> ops);  
  11.         /** updates the first entity found with the operations; if nothing is found insert the update as an entity if "createIfMissing" is true*/  
  12.         <T> UpdateResults<T> updateFirst(Query<T> query, UpdateOperations<T> ops, boolean createIfMissing);  
  13.         /** updates the first entity found with the operations; if nothing is found insert the update as an entity if "createIfMissing" is true*/  
  14.         <T> UpdateResults<T> updateFirst(Query<T> query, T entity, boolean createIfMissing);  
  15. }  
  16. public interface UpdateOperations<T> {  
  17.         /** sets the field value */  
  18.         UpdateOperations<T> set(String fieldExpr, Object value);  
  19.         /** removes the field */  
  20.         UpdateOperations<T> unset(String fieldExpr);  
  21.   
  22.   
  23.         /** adds the value to an array field*/  
  24.         UpdateOperations<T> add(String fieldExpr, Object value);  
  25.         UpdateOperations<T> add(String fieldExpr, Object value, boolean addDups);  
  26.         /** adds the values to an array field*/  
  27.         UpdateOperations<T> addAll(String fieldExpr, List<?> values, boolean addDups);  
  28.           
  29.         /** removes the first value from the array*/  
  30.         UpdateOperations<T> removeFirst(String fieldExpr);  
  31.         /** removes the last value from the array*/  
  32.         UpdateOperations<T> removeLast(String fieldExpr);  
  33.         /** removes the value from the array field*/  
  34.         UpdateOperations<T> removeAll(String fieldExpr, Object value);  
  35.         /** removes the values from the array field*/  
  36.         UpdateOperations<T> removeAll(String fieldExpr, List<?> values);  
  37.   
  38.   
  39.         /** decrements the numeric field by 1*/  
  40.         UpdateOperations<T> dec(String fieldExpr);  
  41.         /** increments the numeric field by 1*/  
  42.         UpdateOperations<T> inc(String fieldExpr);  
  43.         /** increments the numeric field by value (negatives are allowed)*/  
  44.         UpdateOperations<T> inc(String fieldExpr, Number value);  
  45. }  
               
         The Field Expression
            属性表达式是用在所有的操作上的,可以是单个的属性名,也可以是用点“.”连接的嵌套属性。在表达式中你也可以使用位置操作副($)。在属性
          表达式中没有标准,你可以使用任何在MongoDB服务端有效符号。
         
         事例初始化
              一下所有的实例创建的连接和Morphia实例都使用的一下代码。
                    
[java]  view plain copy
  1. Morphia morphia = new Morphia();  
  2. morphia.map(Hotel.class).map(Address.class);  
  3. Datastore datastore = morphia.createDatastore("MorphiaSampleDb");  
  4. Hotel hotel = new Hotel("Fairmont"3new Address("1 Rideau Street""Ottawa""K1N8S7""Canada"));  
  5. datastore.save(hotel);  
  6. UpdateOperations<Hotel> ops;  
  7.   
  8. // This query will be used in the samples to restrict the update operations to only the hotel we just created.  
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值