mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-19 18:22:23 +08:00
Update mongodb
This commit is contained in:
@@ -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> => 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<BsonArray>(str);
|
||||
|
||||
// 3. 获取元素类型,例如 List<MyClass> => 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
|
||||
|
@@ -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<List<Dictionary<string, object>>>(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<List<Dictionary<string, object>>>(json));
|
||||
}
|
||||
}
|
||||
else if (IsBytes(readerValues, item))
|
||||
{
|
||||
@@ -667,8 +677,16 @@ namespace SqlSugar
|
||||
return isArray || isListItem;
|
||||
}
|
||||
|
||||
private static bool IsJsonList(Dictionary<string, object> readerValues, PropertyInfo item)
|
||||
private bool IsJsonList(Dictionary<string, object> 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<string, object> DataReaderToDynamicList_Part<T>(Dictionary<string, object> readerValues, PropertyInfo item, List<T> reval, Dictionary<string, string> mappingKeys = null,List<string> ignoreColumns=null)
|
||||
{
|
||||
Dictionary<string, object> result = new Dictionary<string, object>();
|
||||
|
Reference in New Issue
Block a user