当前位置:Gxlcms > 数据库问题 > MongoDB学习笔记~Update方法更新集合属性后的怪问题

MongoDB学习笔记~Update方法更新集合属性后的怪问题

时间:2021-07-01 10:21:17 帮助过:7人阅读

Task UpdateAsync(TEntity item) { var query = new QueryDocument("_id", typeof(TEntity).GetProperty(EntityKey).GetValue(item).ToString()); var fieldList = new List<UpdateDefinition<TEntity>>(); foreach (var property in typeof(TEntity).GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (property.Name != EntityKey)//更新集中不能有实体键_id { fieldList.Add(Builders<TEntity>.Update.Set(property.Name, property.GetValue(item))); } } return ForWait(() => _table.UpdateOneAsync(query, Builders<TEntity>.Update.Combine(fieldList))); }

确实没看出什么问题来,但最后它生成的代码是以_t和_v为键值的值,出现这种情况的原因是你的代码没有被mongo识别,就像之前我们为mongo传decimal类型的数据一样,它也会出现同样的情况。

解决方法

将复杂类型进行拆封和组装,让它被mongo所认识,这样update操作就可以按着我们预想的完成了,值得注意的是,如果你的对象里有复杂类型,如Person类里有Address类型,那么在赋值时我们拼成以下这样

Address.City="北京"

而如果你的对象里属性为集合类型,那就更麻烦一些,除了做上面的拆封外,还要关注它的索引号,如Person类里有AddList集合属性,那么在赋值时我们拼成以下这样

AddList.0.City="北京"

下面公开大叔的Update代码

        public Task UpdateAsync(TEntity item)
        {
            var query = new QueryDocument("_id", typeof(TEntity).GetProperty(EntityKey).GetValue(item).ToString());
            var fieldList = new List<UpdateDefinition<TEntity>>();
            foreach (var property in typeof(TEntity).GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                //非空的复杂类型
                if (property.PropertyType.IsClass && property.PropertyType != typeof(string) && property.GetValue(item) != null)
                {

                    if (typeof(IList).IsAssignableFrom(property.PropertyType))
                    {
                        #region 集合类型
                        foreach (var sub in property.PropertyType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                        {
                            if (sub.PropertyType.IsClass && sub.PropertyType != typeof(string))
                            {
                                var arr = property.GetValue(item) as IList;
                                if (arr != null && arr.Count > 0)
                                {
                                    for (int s = 0; s < arr.Count; s++)
                                    {
                                        foreach (var subInner in sub.PropertyType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                                        {
                                            //propertyName.index.innerPropertyName
                                            fieldList.Add(Builders<TEntity>.Update.Set(property.Name + "."+ s + "." + subInner.Name, subInner.GetValue(arr[s])));
                                        }
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        #region 实体类型
                        //复杂类型,导航属性,类对象和集合对象 
                        foreach (var sub in property.PropertyType.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                        {
                            fieldList.Add(Builders<TEntity>.Update.Set(property.Name + "." + sub.Name, sub.GetValue(property.GetValue(item))));
                        }
                        #endregion
                    }
                }
                else //简单类型
                {
                    if (property.Name != EntityKey)//更新集中不能有实体键_id
                    {
                        fieldList.Add(Builders<TEntity>.Update.Set(property.Name, property.GetValue(item)));
                    }
                }
            }

            return ForWait(() => _table.UpdateOneAsync(query, Builders<TEntity>.Update.Combine(fieldList)));

        }

希望本文章对使用MongoDB的学生来说有所帮助!

回到目录

 

MongoDB学习笔记~Update方法更新集合属性后的怪问题

标签:

人气教程排行