​回到目录​

在对MongoDB进行封装后,对于Update更新对象里的集合属性时出现了一个现象,让人感到很恶心,人家更新前是个美丽的Array,但是更新之后集合对象变成了键值对,键是集合的类型名称,值是真实的数组值,哈哈,这个问题起初困扰了我很久,今天终于豁然开朗了,原来是Update方法的问题,呵呵!

看原来的值

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

看更新后的变质的值

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

再看看我们的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.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的学生来说有所帮助!

​回到目录​

 

作者:仓储大叔,张占岭,
荣誉:微软MVP