MongoDB學(xue)習筆(bi)記~官方驅(qu)動嵌套(tao)數組對(dui)象的更新
對(dui)于數(shu)組(zu)(zu)(zu)對(dui)象mongodb本身是支(zhi)持(chi)的,不(bu)過對(dui)于數(shu)組(zu)(zu)(zu)的更(geng)新,mongodb的Csharp驅(qu)(qu)動(dong)(dong)目(mu)前只支(zhi)持(chi)一級(ji),即你的對(dui)象里包含數(shu)組(zu)(zu)(zu),而數(shu)組(zu)(zu)(zu)又包括數(shu)組(zu)(zu)(zu),這表示兩層,這在(zai)更(geng)新子數(shu)組(zu)(zu)(zu)時,Csharp驅(qu)(qu)動(dong)(dong)是不(bu)支(zhi)持(chi)的,今天要說的就是如(ru)何讓它支(zhi)持(chi)子數(shu)組(zu)(zu)(zu)的更(geng)新,下面是我給出的數(shu)據結構
在(zai)Mongodb的(de)Csharp驅動里(li),一(yi)般的(de)更(geng)新方法如(ru)下
update = new UpdateDocument {{ "$set", new BsonDocument("OrderList.$.UserName","占占的訂(ding)單") }}; mongoRepository.Collection.Update(query, update);
上面(mian)代碼可以很快速的將指定的二級數(shu)組OrderList里的UserName字段更新,而如果你要更新OrderDetail就沒那么容(rong)易(yi)了,Csharp驅支目(mu)前是不支持的,當然你肯(ken)定會照著葫(hu)蘆畫(hua)飄,但(dan)結(jie)果是失(shi)(shi)敗(bai)的,就像下面(mian)的代碼(失(shi)(shi)敗(bai),只有既望新版驅動了)
update = new UpdateDocument {{ "$set", new BsonDocument("OrderList.$.OrderDetail.ProductName","占(zhan)占(zhan)") }}; mongoRepository.Collection.Update(query, update);
結果是OrderDetail的(de)ProductName沒有發生任何變化,呵(he)呵(he)。
去找(zhao)原因(yin),去找(zhao)資(zi)料,還好,找(zhao)到了一個(ge)(ge)不錯的(de)(de)說法,即要想更新數組(zu)內的(de)(de)數組(zu),需要將對(dui)上(shang)級數據進行定位,如OrderList.0.OrderDetail,這表示下標為0的(de)(de)OrderList數組(zu)元素(一個(ge)(ge)實體)下的(de)(de)OrderDetail數組(zu)對(dui)象(xiang),當然這個(ge)(ge)下標可不是那么輕易(yi)能得(de)到的(de)(de),
我(wo)們需要對數組進(jin)行遍(bian)歷,找到(dao)滿足條(tiao)件(jian)的(de)后,進(jin)行break即(ji)可。
var mongoRepository = new MongoDB.Data.Core.MongoOfficialRepository<Person>(); var query = Query.EQ("OrderList._id", twoID); var oo = mongoRepository.Collection.Find(query).FirstOrDefault(); var update = new UpdateDocument(); bool isExit = false; for (int j = 0; j < oo.OrderList.Count; j++) { var od = oo.OrderList[j].OrderDetail; oo.OrderList[j].UserName = "大(da)占占改呀"; for (int i = 0; i < od.Count; i++) { if (od[i].Id == threeID) { od[i].ProductName = "大(da)占占修改了訂單21"; #region 先pull,再push //update = new UpdateDocument {{ "$pull", // new BsonDocument("OrderList."+j+".OrderDetail", // new BsonDocument("_id",threeID)) // }}; //mongoRepository.Collection.Update(query1, update); //update = new UpdateDocument {{ "$push", // new BsonDocument("OrderList."+j+".OrderDetail", // new BsonDocument(od[i].ToDictionary())) // }}; //mongoRepository.Collection.Update(query1, update); #endregion #region 直接set update = new UpdateDocument {{ "$set", new BsonDocument("OrderList.$.UserName",oo.OrderList[j].UserName) }}; mongoRepository.Collection.Update(query, update); update = new UpdateDocument {{ "$set", new BsonDocument("OrderList."+j+".OrderDetail."+i, new BsonDocument(od[i].ToDictionary())) }}; mongoRepository.Collection.Update(query, update); #endregion isExit = true; break; } } if (isExit) break; }
上面的(de)(de)代(dai)碼,我(wo)們看到了(le)有兩種更新(xin)集合的(de)(de)方(fang)(fang)法(fa),$pull和$push方(fang)(fang)法(fa)及$set方(fang)(fang)法(fa),大(da)家可(ke)以(yi)根據喜好進行選擇,都可(ke)以(yi)實現我(wo)們的(de)(de)目的(de)(de),ToDictionary是(shi)我(wo)封裝的(de)(de)方(fang)(fang)法(fa),意思是(shi)將(jiang)類(lei)對象里的(de)(de)屬(shu)性轉換(huan)為字典,并做了(le)mongodb的(de)(de)_id主鍵的(de)(de)處(chu)理,代(dai)碼如下
/// <summary> /// 將對象(xiang)屬(shu)性轉換為字典 /// </summary> /// <param name="o"></param> /// <returns></returns> public static Dictionary<String, Object> ToDictionary(this object o) { Dictionary<String, Object> map = new Dictionary<string, object>(); Type t = o.GetType(); PropertyInfo[] pi = t.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo p in pi) { MethodInfo mi = p.GetGetMethod(); if (mi != null && mi.IsPublic) { if (p.Name == "Id") map.Add("_id", mi.Invoke(o, new Object[] { })); else map.Add(p.Name, mi.Invoke(o, new Object[] { })); } } return map; } }
對于mongodb的(de)探索(suo),還在繼續...