隨筆 - 22, 文章 - 0, 評論 - 1, 引用 - 0
          數據加載中……

          mongodb的morphia框架學習筆記(補充)

          morphia的更新操作詳解:
          -------------------------
          用id字段匹配是最快的,因為mongodb默認為id列做了索引:
          Query<Hotel> updateQuery = datastore.createQuery(Hotel.class).field("_id").equal(hotel.getId());
          除了"_id"外,還有一種靜態常量描述ID的方式:
          Query<Hotel> updateQuery = datastore.createQuery(Hotel.class).field(Mapper.ID_KEY).equal(hotel.getId());
          注意,用的是equal()方法,而不是equals()方法!
          -------------------------
          set和unset方法:
          改旅店名:
          ops = datastore.createUpdateOperations(Hotel.class).set("name", "Fairmont Chateau Laurier");
          改旅店地址名:
          ops = datastore.createUpdateOperations(Hotel.class).set("address.city", "Ottawa");
          刪除name屬性,會導致下次讀取時name=null
          ops = datastore.createUpdateOperations(Hotel.class).unset("name");
          -------------------------
          inc和dec方法:
          星級加1:
          ops = datastore.createUpdateOperations(Hotel.class).inc("stars");
          星級加4:
          ops = datastore.createUpdateOperations(Hotel.class).inc("stars", 4);
          -------------------------
          add和addAll方法:
          將11加入房間號數組中:
          ops = datastore.createUpdateOperations(Hotel.class).add("roomNumbers", 11);
          這與add("roomNumbers", 11, false)等價
          在非數組字段上執行add操作將報錯。
          add的第三個參數標識“是否加入重復元素”。若為true,如果已存在相同元素,不會加入,也不會報錯。
          -------------------------
          removeFirst/Last/All方法:
          假設目前是[ 1, 2, 3, 3 ],運行以下方法之后:
          ops = datastore.createUpdateOperations(Hotel.class).removeAll("roomNumbers", 3);
          就剩下了[ 1, 2 ]——所有的3都被移除了。
          假設目前是[ 1, 2, 3, 3 ],運行以下方法之后:
          ops = datastore.createUpdateOperations(Hotel.class).removeAll("roomNumbers", Arrays.asList(2, 3));
          就剩下了[ 1 ] ——所有的2和3都被移除了。
          -------------------------
          多重操作:
          ops = datastore.createUpdateOperations(Hotel.class).set("city", "Ottawa").inc("stars");
          如果在同一個updateOperation對象上對同一個字段多次操作,結果是不定的。
          -------------------------
          update/updateFirst方法:
          在默認driver里,使用update默認只更新第一個元素(multi默認為false)。
          但是在morphia里,update影響所有記錄(對應的底層driver里的update第四個參數multi為true),updateFirst才是影響第一個元素(對應的底層driver里的update第四個參數multi為false)。
          -------------------------
          createIfMissing參數:
          morphia里所有的方法都重載,最后可以附加一個參數“createIfMissing”
          對應的底層driver或者shell里的操作是upsert = true:
          db.collection.update( criteria, objNew, true, multi );
          -------------------------------------------------
          樂觀鎖注解@Version的使用:
          http://code.google.com/p/morphia/wiki/MongoNewsletterArticleDec2010
          class Person {
            @Id String name;
            String phone;
            @Version
            long version;
          }
          Person me = new Person("Scott Hernandez");
          ds.save(me) //保存一個person
          Person meAgain = ds.get(Person.class, "Scott Hernandez"); //把這個person讀出來先放著
          me.setPhone("111-376-7379"); 
          ds.save(me); //把原來的me修改一下再存進去,版本號已經變化
          meAgain.setPhone("123-376-7379");
          ds.save(meAgain); //剛次讀出來的meAgain是老版本號,此時存儲將拋出并發異常
          注:目前樂觀鎖的版本號實現方式是時間戳,根據作者的回答:
          http://code.google.com/p/morphia/wiki/MongoNewsletterArticleDec2010
          下一個版本的morphia將使用自增數字來代替時間戳作為樂觀鎖的版本號使用,以避免多服務器時間不一致等問題。

          posted on 2012-09-21 17:19 王星游 閱讀(2465) 評論(0)  編輯  收藏 所屬分類: java

          主站蜘蛛池模板: 平阴县| 漠河县| 沙河市| 闽清县| 普兰店市| 铜川市| 海宁市| 越西县| 会宁县| 克什克腾旗| 清苑县| 招远市| 涟源市| 万安县| 长汀县| 田阳县| 丹巴县| 澎湖县| 太保市| 普洱| 陇西县| 光山县| 麻城市| 昌乐县| 儋州市| 百色市| 丰镇市| 吴旗县| 陈巴尔虎旗| 铜鼓县| 樟树市| 上林县| 博乐市| 洮南市| 峡江县| 嘉鱼县| 昌乐县| 大冶市| 武隆县| 玛多县| 大城县|