diff --git a/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/SqlBuilder/MongoDbInsertBuilder.cs b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/SqlBuilder/MongoDbInsertBuilder.cs index c3c3bf286..b82939c9b 100644 --- a/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/SqlBuilder/MongoDbInsertBuilder.cs +++ b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/SqlBuilder/MongoDbInsertBuilder.cs @@ -116,7 +116,7 @@ namespace SqlSugar.MongoDb } return resultList; } - else if (json is BsonArray array&&type.GetGenericArguments().Any()) + else if (json is BsonArray array && type.GetGenericArguments().Any()) { var bsonArray = array; @@ -135,23 +135,23 @@ namespace SqlSugar.MongoDb var obj = BsonSerializer.Deserialize(doc, elementType); resultList.Add(obj); } - else + else { - var obj = MongoDbDataReaderHelper.ConvertBsonValue(item); + var obj = MongoDbDataReaderHelper.ConvertBsonValue(item); resultList.Add(obj); } } return resultList; } else if (json is BsonArray array2 && type.IsArray) - { + { var bsonArray = array2; // 3. 获取元素类型,例如 List => MyClass Type elementType = type.GetElementType(); // 4. 构造泛型列表对象 - var resultList= Array.CreateInstance(elementType, array2.Count()); + var resultList = Array.CreateInstance(elementType, array2.Count()); // 5. 反序列化每一项 int index = 0; @@ -168,7 +168,35 @@ namespace SqlSugar.MongoDb resultList.SetValue(obj, index); } index++; - } + } + return resultList; + } + else if (json is string str && type.GetGenericArguments().Any()) + { + + var jsonArray = MongoDB.Bson.Serialization.BsonSerializer.Deserialize(str); + + // 3. 获取元素类型,例如 List => MyClass + Type elementType = type.GetGenericArguments()[0]; + + // 4. 构造泛型列表对象 + var resultList = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType)); + + // 5. 反序列化每一项 + foreach (var item in jsonArray) + { + if (item is BsonDocument) + { + var doc = item.AsBsonDocument; + var obj = BsonSerializer.Deserialize(doc, elementType); + resultList.Add(obj); + } + else + { + var obj = MongoDbDataReaderHelper.ConvertBsonValue(item); + resultList.Add(obj); + } + } return resultList; } else diff --git a/Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs b/Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs index d010015e1..d4543580b 100644 --- a/Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs +++ b/Src/Asp.NetCore2/SqlSugar/Infrastructure/ContextMethods.cs @@ -8,6 +8,7 @@ using System.Dynamic; using System.Linq; using System.Linq.Expressions; using System.Reflection; +using System.Runtime.CompilerServices; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -493,7 +494,16 @@ namespace SqlSugar else if (IsJsonList(readerValues, item)) { var json = readerValues.First(y => y.Key.EqualCase(item.Name)).Value.ToString(); - result.Add(name, DeserializeObject>>(json)); + if (IsMongoDb()) + { + var q=InstanceFactory.GetInsertBuilder(this.Context.CurrentConnectionConfig); + var qv =q.DeserializeObjectFunc(json, item.PropertyType); + result.Add(name, qv); + } + else + { + result.Add(name, DeserializeObject>>(json)); + } } else if (IsBytes(readerValues, item)) { @@ -667,8 +677,16 @@ namespace SqlSugar return isArray || isListItem; } - private static bool IsJsonList(Dictionary readerValues, PropertyInfo item) + private bool IsJsonList(Dictionary readerValues, PropertyInfo item) { + if (IsMongoDb()) + { + return item.PropertyType.FullName.IsCollectionsList() && + readerValues.Any(y => y.Key.EqualCase(item.Name)) && + readerValues.First(y => y.Key.EqualCase(item.Name)).Value != null && + readerValues.First(y => y.Key.EqualCase(item.Name)).Value.GetType().FullName == "MongoDB.Bson.BsonArray" && + Regex.IsMatch(readerValues.First(y => y.Key.EqualCase(item.Name)).Value.ToString(), @"^\[{.+\}]$"); + } return item.PropertyType.FullName.IsCollectionsList() && readerValues.Any(y => y.Key.EqualCase(item.Name)) && readerValues.First(y => y.Key.EqualCase(item.Name)).Value != null && @@ -676,6 +694,11 @@ namespace SqlSugar Regex.IsMatch(readerValues.First(y => y.Key.EqualCase(item.Name)).Value.ToString(), @"^\[{.+\}]$"); } + private bool IsMongoDb() + { + return this.Context?.CurrentConnectionConfig?.DbType == DbType.MongoDb; + } + private Dictionary DataReaderToDynamicList_Part(Dictionary readerValues, PropertyInfo item, List reval, Dictionary mappingKeys = null,List ignoreColumns=null) { Dictionary result = new Dictionary();