Mongodb 的ORM框架 Morphia 之 Updating

转自:http://blog.csdn.net/small_love/article/details/6628200


           简介
           这个有两个修改你的数据库的方法。你可以insert/save一个完整的实体,或者通过update操作。我们将在一下讨论。
         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.  
  9. // If this was not supplied, by default the update() operates on all documents in the collection.  
  10. // We could use any field here but _id will be unique and mongodb by default puts an index on the _id field so this should be fast!  
  11. Query<Hotel> updateQuery = datastore.createQuery(Hotel.class).field("_id").equal(hotel.getId());  
  12.   
  13. // The Mapper class also provides a public static of the default _id field name for us...  
  14. Query<Hotel> updateQuery = datastore.createQuery(Hotel.class).field(Mapper.ID_KEY).equal(hotel.getId());  

         注意: 使用的是 equal() 而不是 equals()
              
[java]  view plain copy
  1. @Entity("hotels")  
  2. public class Hotel  
  3. {  
  4.    @Id  
  5.    private ObjectId id;  
  6.   
  7.   
  8.    private String name;  
  9.    private int stars;  
  10.   
  11.   
  12.    @Embedded  
  13.    private Address address;  
  14.   
  15.   
  16.    @Embedded  
  17.    List<Integer> roomNumbers = new ArrayList<Integer>();  
  18.   
  19.   
  20.    // ... getters and setters  
  21. }  
  22.   
  23.   
  24. @Embedded  
  25. public class Address  
  26. {  
  27.    private String street;  
  28.    private String city;  
  29.    private String postalCode;  
  30.    private String country;  
  31.   
  32.   
  33.    // ... getters and setters  
  34. }  
      
      set/unset
            
[java]  view plain copy
  1. // 改变Hotel的name属性值  
  2. ops = datastore.createUpdateOperations(Hotel.class).set("name""Fairmont Chateau Laurier");  
  3. datastore.update(updateQuery, ops);  
  4.   
  5. //也可以操作嵌套文档, 改变address的city属性值  
[java]  view plain copy
  1. ops = datastore.createUpdateOperations(Hotel.class).set("address.city""Ottawa");  
[java]  view plain copy
  1. datastore.update(updateQuery, ops);  
  2.   
  3. // 删除Hotel的name属性值  
  4. // 当下会访问Hotel时name属性为null  
  5. ops = datastore.createUpdateOperations(Hotel.class).unset("name");  
  6. datastore.update(updateQuery, ops);  
   
       inc/dec
          
[java]  view plain copy
  1. // 'stars'属性增长一  
  2. ops = datastore.createUpdateOperations(Hotel.class).inc("stars");  
  3. datastore.update(updateQuery, ops);  
  4.   
  5. // 'stars'属性值增长4  
  6. ops = datastore.createUpdateOperations(Hotel.class).inc("stars"4);  
  7. datastore.update(updateQuery, ops);  
  8.   
  9. // 'stars'属性值减少1  
  10. ops = datastore.createUpdateOperations(Hotel.class).dec("stars");  // 和 .inc("stars", -1) 相同  
  11. datastore.update(updateQuery, ops);  
  12.   
  13. // 'stars'属性值减少4  
  14. ops = datastore.createUpdateOperations(Hotel.class).inc("stars", -4);  
  15. datastore.update(updateQuery, ops);  
    
       add/All
            
[java]  view plain copy
  1. // 把一个值放入到数组中 array() (+v 0.95)   
  2. // same as .add("roomNumbers", 11, false)  
  3. ops = datastore.createUpdateOperations(Hotel.class).add("roomNumbers"11);  
  4. datastore.update(updateQuery, ops); // [ 11 ]  
    当在一个不是数组的属性上进行数组操作时MongoDB将会抛出错误。
      
[java]  view plain copy
  1. ops = datastore.createUpdateOperations(Hotel.class).set("roomNumbers"11);  
  2. datastore.update(updateQuery, ops);  
  3.   
  4. // 由于没有rooNumbers数组将会引起错误  
  5. ops = datastore.createUpdateOperations(Hotel.class).add("roomNumbers"11false);  
  6. datastore.update(updateQuery, ops);  // causes error  
  7.   
  8. // 删除roomNummbers属性  
  9. ops = datastore.createUpdateOperations(Hotel.class).unset("roomNumbers");  
  10. datastore.update(updateQuery, ops);  
  11.   
  12. // use the 3rd parameter to add duplicates  
  13.   
  14. // add to end of array, same as add()  
  15. ops = datastore.createUpdateOperations(Hotel.class).add("roomNumbers"11false);  
  16. datastore.update(updateQuery, ops);  // [ 11 ]  
  17.   
  18. // no change since its a duplicate... doesn't cause error  
  19. ops = datastore.createUpdateOperations(Hotel.class).add("roomNumbers"11false);  
  20. datastore.update(updateQuery, ops);  // [ 11 ]  
  21.   
  22. // push onto the end of the array  
  23. ops = datastore.createUpdateOperations(Hotel.class).add("roomNumbers"12false);  
  24. datastore.update(updateQuery, ops); // [ 11, 12 ]  
  25.   
  26. // add even if its a duplicate  
  27. ops = datastore.createUpdateOperations(Hotel.class).add("roomNumbers"11true);  
  28. datastore.update(updateQuery, ops); // [ 11, 12, 11 ]  

      removeFirst/Last/All

            
[java]  view plain copy
  1. //given roomNumbers = [ 1, 2, 3 ]  
  2. ops = datastore.createUpdateOperations(Hotel.class).removeFirst("roomNumbers");  
  3. datastore.update(updateQuery, ops);  // [ 2, 3 ]  
  4.   
  5.   
  6. //given roomNumbers = [ 1, 2, 3 ]  
  7. ops = datastore.createUpdateOperations(Hotel.class).removeLast("roomNumbers");  
  8. datastore.update(updateQuery, ops);  // [ 1, 2 ]  
  9. ops = datastore.createUpdateOperations(Hotel.class).removeLast("roomNumbers");  
  10. datastore.update(updateQuery, ops);  // [ 1 ]  
  11. ops = datastore.createUpdateOperations(Hotel.class).removeLast("roomNumbers");  
  12. datastore.update(updateQuery, ops);  // []   empty array  
  13.   
  14.   
  15. //given roomNumbers = [ 1, 2, 3, 3 ]  
  16. ops = datastore.createUpdateOperations(Hotel.class).removeAll("roomNumbers"3);  
  17. datastore.update(updateQuery, ops);  // [ 1, 2 ]  
  18.   
  19. //given roomNumbers = [ 1, 2, 3, 3 ]  
  20. ops = datastore.createUpdateOperations(Hotel.class).removeAll("roomNumbers", Arrays.asList(23));  
  21. datastore.update(updateQuery, ops);  // [ 1 ]  
    

   Multiple Operations

           你也可以在一个update指令中执行多个updae操作。
           
[java]  view plain copy
  1. //设置城市名称为Ottawa和是stars的增长1  
  2. ops = datastore.createUpdateOperations(Hotel.class).set("city""Ottawa").inc("stars");  
  3. datastore.update(updateQuery, ops);  
  4.   
  5. //如果你在同一个属性上执行多次同样的指令操作,结果将会变化,即:只有最后一次有效  
  6. ops = datastore.createUpdateOperations(Hotel.class).inc("stars"50).inc("stars");  //stars只增长1  
  7. ops = datastore.createUpdateOperations(Hotel.class).inc("stars").inc("stars"50);  //stars只增长50  
  8.   
  9. //你不能在同一个属性上执行相矛盾的操作。  
  10. ops = datastore.createUpdateOperations(Hotel.class).set("stars"1).inc("stars"50); //引起错误  
       updateFirst方法            在默认的驱动和shell上这是默认的行为。在Morphia中我们认为修改所有的符合条件的结果是最好的默认选择(如下)。
              {name: "Fairmont", stars: 5}, {name: "Last Chance", stars: 3}
              
[java]  view plain copy
  1. ops = datastore.createUpdateOperations(Hotel.class).inc("stars"50);  
  2.   
  3. // (+v 0.95 now takes into account the order())  
  4. // morphia 执行updateFirst方法仅仅执行第一个符合查询条件的数据项  
  5. datastore.updateFirst(datastore.find(Hotel.class).order("stars"), ops);  //仅仅修改Last Chance  
  6. datastore.updateFirst(datastore.find(Hotel.class).order("-stars"), ops); // 仅仅修改 Fairmont  
[java]  view plain copy
  1. //default shell version is to match first  
  2. //shell version has a multi to indicate to update all matches, not just first  
  3. //to mimic morphia operation, set multi = false  
  4. db.collection.update( criteria, objNew, upsert, multi );  

      update 方法
            
[java]  view plain copy
  1. ops = datastore.createUpdateOperations(Hotel.class).inc("stars"50);  
  2.   
  3. // morphia 默认的update是修改所有的Hotels  
  4. datastore.update(datastore.createQuery(Hotel.class), ops);  //所有的hotel都会增长  
[java]  view plain copy
  1. //equivalent morphia shell version is... upsert = false, multi = true  
  2. db.collection.update( criteria, objNew, falsetrue );  
     createIfMissing (overload parameter)
        所有的update都被重载支持一个"createIfMissing"参数。
       
[java]  view plain copy
  1. ops = datastore.createUpdateOperations(Hotel.class).inc("stars"50);  
  2.   
  3. //修改, 如果没有找到就添加一个。  
  4. datastore.updateFirst(datastore.createQuery(Hotel.class).field("stars").greaterThan(100), ops, true);    
  5.   
  6. // creates { "_id" : ObjectId("4c60629d2f1200000000161d"), "stars" : 50 }  
  7. //equivalent morphia shell version is... upsert = true  
  8. db.collection.update( criteria, objNew, true, multi );  
      英语水平有限,敬请大侠 斧正

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值