MongoDB学习笔记~官方驱动嵌套数组对象的更新

回到目录

对于数组对象mongodb本身是支持的,不过对于数组的更新,mongodb的Csharp驱动目前只支持一级,即你的对象里包含数组,而数组又包括数组,这表示两层,这在更新子数组时,Csharp驱动是不支持的,今天要说的就是如何让它支持子数组的更新,下面是我给出的数据结构

在Mongodb的Csharp驱动里,一般的更新方法如下

  update = new UpdateDocument {{ "$set",       
                                  new BsonDocument("OrderList.$.UserName","占占的订单")
                              }};
  mongoRepository.Collection.Update(query, update);

上面代码可以很快速的将指定的二级数组OrderList里的UserName字段更新,而如果你要更新OrderDetail就没那么容易了,Csharp驱支目前是不支持的,当然你肯定会照着葫芦画飘,但结果是失败的,就像下面的代码(失败,只有既望新版驱动了)

  update = new UpdateDocument {{ "$set",       
                                  new BsonDocument("OrderList.$.OrderDetail.ProductName","占占")
                              }};
  mongoRepository.Collection.Update(query, update);

结果是OrderDetail的ProductName没有发生任何变化,呵呵。

去找原因,去找资料,还好,找到了一个不错的说法,即要想更新数组内的数组,需要将对上级数据进行定位,如OrderList.0.OrderDetail,这表示下标为0的OrderList数组元素(一个实体)下的OrderDetail数组对象,当然这个下标可不是那么轻易能得到的,

我们需要对数组进行遍历,找到满足条件的后,进行break即可。

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 = "大占占改呀";
                for (int i = 0; i < od.Count; i++)
                {
                    if (od[i].Id == threeID)
                    {
                        od[i].ProductName = "大占占修改了订单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;
            }

上面的代码,我们看到了有两种更新集合的方法,$pull和$push方法及$set方法,大家可以根据喜好进行选择,都可以实现我们的目的,ToDictionary是我封装的方法,意思是将类对象里的属性转换为字典,并做了mongodb的_id主键的处理,代码如下

/// <summary>
        /// 将对象属性转换为字典
        /// </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的探索,还在继续...

回到目录

posted @ 2015-08-03 16:47  张占岭  阅读(6075)  评论(1编辑  收藏  举报