MongoDB學(xue)習(xi)筆(bi)記~對集合屬(shu)性的操作(zuo)
$unset清除元素
請(qing)注意在單個數(shu)組元(yuan)素(su)上使用$unset的結(jie)果可能與你設想(xiang)的不一(yi)樣(yang)。其(qi)結(jie)果只是將元(yuan)素(su)的值設置為null,而非(fei)刪除(chu)整個元(yuan)素(su)。要想(xiang)徹(che)底刪除(chu)某個數(shu)組元(yuan)素(su),可以用$pull 和(he)$pop操作符。
$addToSet和$push的區別
該兩(liang)者(zhe)的功能都是(shi)給數組添加一個(ge)值。但是(shi)兩(liang)者(zhe)之間有區別,$addToSet要添加的值如果不存(cun)在才進行添加操作(zuo),但是(shi)push只添加一個(ge)值;例如:
tags = [“zzl”,”dudu”]
如果(guo)執(zhi)行(xing)db.collection.update({},{$push:{tag:"laozhao"}}) 結果(guo)就是 [“laozhao”,”zzl”,“dudu”]
如果(guo)(guo)執行db.collection.update({},{$addToSet:{tag:"zzl"}}) 結果(guo)(guo)不(bu)變(bian)
關于Update.Combine集合更新注意點
對于Update.Combine我們可以把需要更新的字段合并到列表List<UpdateDefinition<T>>()中,最后一起進行更新,而這對于集合屬性來說,需要注意一下,我們需要為集(ji)合屬(shu)性(xing)元素使用PushEach進行(xing)添(tian)加,而不是Push,因(yin)為(wei)使(shi)用Push會將前一個元素(su)覆蓋掉(diao),而只保(bao)留最后的元素(su)(集合里的),所以(yi)需要使(shi)用PushEach來(lai)代替它(ta),代碼如下:
[TestMethod] public void Push() { var filter = Builders<Dog>.Filter.Eq(i => i.Id, "5850b0bdebb91a3184f90d3d"); //更新(xin)所需要的字段 var updateList = new List<UpdateDefinition<Dog>>(); //更新需要集合類型的字段(duan) var dogHistoryList = new List<DogHistory>(); //添加(jia)元素到集合屬性 dogHistoryList.Add(new DogHistory { HistoryName = "四虎子3", IsHealth = false, Adderss = new Adderss("廣(guang)東", "深(shen)圳", "沿海(hai)") }); dogHistoryList.Add(new DogHistory { HistoryName = "四虎子4", IsHealth = false, Adderss = new Adderss("廣(guang)東", "深圳", "沿海(hai)") }); //將(jiang)需要(yao)更新集合對象添加到(dao)updateList里(li) updateList.Add(Builders<Dog>.Update.PushEach(i => i.DogHistory, dogHistoryList)); MongoDbClient.MongoManager<Dog>.Instance.UpdateOne( filter, Builders<Dog>.Update.Combine(updateList)); }
Update倉儲的優化
大(da)叔對于(yu)這(zhe)一點,也把(ba)它(ta)封(feng)裝到了(le)Lind.DDD.Repositories.Mongo的倉儲里,完善了(le)Update操作(zuo),修(xiu)改了(le)之前的遞歸處理(li)(li)邏(luo)輯,代(dai)碼反而更(geng)簡潔了(le),原理(li)(li)就是使用(yong)$set直接把(ba)原數據覆蓋即(ji)可。
/// <summary> /// 版本(ben)二:遞歸構建Update操作串 /// 主要(yao)功能:實現List子屬性的push操作 /// </summary> /// <param name="fieldList"></param> /// <param name="property"></param> /// <param name="propertyValue"></param> /// <param name="item"></param> /// <param name="father"></param> private void GenerateRecursionSet( List<UpdateDefinition<TEntity>> fieldList, PropertyInfo property, object propertyValue, TEntity item, string father ) { //復雜類型 if (property.PropertyType.IsClass && property.PropertyType != typeof(string) && propertyValue != null) { //集合 if (typeof(IList).IsAssignableFrom(propertyValue.GetType())) { var arr = propertyValue as IList; if (arr != null && arr.Count > 0) { if (string.IsNullOrWhiteSpace(father)) fieldList.Add(Builders<TEntity>.Update.Set(property.Name, arr)); else fieldList.Add(Builders<TEntity>.Update.Set(father + "." + property.Name, arr)); } } //實體 else { foreach (var sub in property.PropertyType.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (string.IsNullOrWhiteSpace(father)) GenerateRecursionSet(fieldList, sub, sub.GetValue(propertyValue), item, property.Name); else GenerateRecursionSet(fieldList, sub, sub.GetValue(propertyValue), item, father + "." + property.Name); } } } //簡單類型 else { if (property.Name != EntityKey)//更新集中不能有(you)實體鍵_id { if (string.IsNullOrWhiteSpace(father)) fieldList.Add(Builders<TEntity>.Update.Set(property.Name, propertyValue)); else fieldList.Add(Builders<TEntity>.Update.Set(father + "." + property.Name, propertyValue)); } } }
對于(yu)產生的結(jie)果是我(wo)們可以接受(shou)的,可以對集合屬(shu)性很方(fang)便的實現更新(xin)了。
dog.Des.Worker = new string[] { "engineer", "coder" }; dog.Des.Address = new List<Adderss> { new Adderss("beijing","fangshan","liangxiang",new string[]{"zhaojiaogan","Road100","No.300"}), new Adderss("北京","大興","西(xi)紅門",new string[]{"理(li)想城","大(da)滿(man)貫","4號樓"}), };
產生的結果如下
以上數(shu)據結構(gou)應(ying)該算是比(bi)較(jiao)復雜(za)的(de)了,像實體(ti)里(li)有子(zi)實體(ti),然后子(zi)實體(ti)里(li)有集合,集合里(li)又有數(shu)組(zu),但我封裝的(de)更新還是都(dou)適用(yong)的(de),這(zhe)點(dian)已經做(zuo)過測試(shi),請放心使用(yong)!
歡迎大(da)家繼續關(guan)注mongodb技(ji)術!
繼續關注大叔博客!