mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
Update mongodb
This commit is contained in:
parent
446af2afad
commit
b8d1ae414d
@ -15,6 +15,7 @@ namespace MongoDbTest
|
|||||||
QueryWhere.Init();
|
QueryWhere.Init();
|
||||||
QuerySelect.Init();
|
QuerySelect.Init();
|
||||||
QueryJson.Init();
|
QueryJson.Init();
|
||||||
|
QueryJsonArray.Init();
|
||||||
Insert.Init();
|
Insert.Init();
|
||||||
Update.Init();
|
Update.Init();
|
||||||
Delete.Init();
|
Delete.Init();
|
||||||
|
45
Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJsonArray.cs
Normal file
45
Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJsonArray.cs
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
using SqlSugar.MongoDb;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbTest
|
||||||
|
{
|
||||||
|
public class QueryJsonArray
|
||||||
|
{
|
||||||
|
internal static void Init()
|
||||||
|
{
|
||||||
|
var db = DBHelper.DbHelper.GetNewDb();
|
||||||
|
db.CodeFirst.InitTables<Student>();
|
||||||
|
db.DbMaintenance.TruncateTable<Student>();
|
||||||
|
db.Insertable(new Student() { Age = 1, Name = "tom", SchoolId = "a", Book = new List<Book>() { new Book() { CreateTime = DateTime.Now, Price = 21 } } }).ExecuteCommand();
|
||||||
|
var data1=db.Queryable<Student>().ToList();
|
||||||
|
if (data1.First().Book.Count != 1) Cases.ThrowUnitError();
|
||||||
|
if (data1.First().Book.First().Price != 21) Cases.ThrowUnitError();
|
||||||
|
}
|
||||||
|
|
||||||
|
[SqlSugar.SugarTable("UnitStudentdfsds3zzz1")]
|
||||||
|
public class Student : MongoDbBase
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public string SchoolId { get; set; }
|
||||||
|
|
||||||
|
public int Age { get; set; }
|
||||||
|
|
||||||
|
public DateTime CreateDateTime { get; set; }
|
||||||
|
|
||||||
|
[SqlSugar.SugarColumn(IsJson = true)]
|
||||||
|
public List<Book> Book { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Book
|
||||||
|
{
|
||||||
|
public decimal Price { get; set; }
|
||||||
|
public DateTime CreateTime { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -9,6 +9,7 @@ using System.Linq;
|
|||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Nodes;
|
||||||
|
|
||||||
namespace SqlSugar.MongoDb
|
namespace SqlSugar.MongoDb
|
||||||
{
|
{
|
||||||
@ -22,10 +23,18 @@ namespace SqlSugar.MongoDb
|
|||||||
|
|
||||||
if (value is IEnumerable enumerable)
|
if (value is IEnumerable enumerable)
|
||||||
{
|
{
|
||||||
var realType = value.GetType();
|
var list = new List<BsonDocument>();
|
||||||
var bson = value.ToBson(realType);
|
|
||||||
var json = bson.ToJson(UtilMethods.GetJsonWriterSettings());
|
foreach (var e in enumerable)
|
||||||
return json;
|
{
|
||||||
|
var realType = e.GetType();
|
||||||
|
var bson = e.ToBson(realType); // 序列化为 byte[]
|
||||||
|
var doc = BsonSerializer.Deserialize<BsonDocument>(bson); // 反序列化为 BsonDocument
|
||||||
|
list.Add(doc);
|
||||||
|
}
|
||||||
|
|
||||||
|
var array = new BsonArray(list);
|
||||||
|
return array.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -51,9 +60,30 @@ namespace SqlSugar.MongoDb
|
|||||||
// 再用 BsonSerializer 反序列化为 T
|
// 再用 BsonSerializer 反序列化为 T
|
||||||
return BsonSerializer.Deserialize(bsonDoc, type);
|
return BsonSerializer.Deserialize(bsonDoc, type);
|
||||||
}
|
}
|
||||||
|
else if (json is List<object> list)
|
||||||
|
{
|
||||||
|
string jsonStr = System.Text.Encoding.UTF8.GetString(list.Select(it=>Convert.ToByte(it)).ToArray());
|
||||||
|
// 2. 解析为 BsonArray
|
||||||
|
var bsonArray = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonArray>(jsonStr);
|
||||||
|
|
||||||
|
// 3. 获取元素类型,例如 List<MyClass> => MyClass
|
||||||
|
Type elementType = type.GetGenericArguments()[0];
|
||||||
|
|
||||||
|
// 4. 构造泛型列表对象
|
||||||
|
var resultList = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType));
|
||||||
|
|
||||||
|
// 5. 反序列化每一项
|
||||||
|
foreach (var item in bsonArray)
|
||||||
|
{
|
||||||
|
var doc = item.AsBsonDocument;
|
||||||
|
var obj = BsonSerializer.Deserialize(doc, elementType);
|
||||||
|
resultList.Add(obj);
|
||||||
|
}
|
||||||
|
return resultList;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return null;
|
return json;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -108,7 +138,7 @@ namespace SqlSugar.MongoDb
|
|||||||
// 自动推断类型,如 string、int、bool、DateTime、ObjectId 等
|
// 自动推断类型,如 string、int、bool、DateTime、ObjectId 等
|
||||||
if (col.IsJson == true)
|
if (col.IsJson == true)
|
||||||
{
|
{
|
||||||
doc[col.DbColumnName] = BsonDocument.Parse(col.Value?.ToString());
|
doc[col.DbColumnName] =UtilMethods.ParseJsonObject(col.Value);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Bson.Serialization;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
@ -17,6 +18,19 @@ namespace SqlSugar.MongoDb
|
|||||||
{
|
{
|
||||||
public class UtilMethods
|
public class UtilMethods
|
||||||
{
|
{
|
||||||
|
public static BsonValue ParseJsonObject(object json)
|
||||||
|
{
|
||||||
|
if (json is string str && str.TrimStart().StartsWith("{"))
|
||||||
|
{
|
||||||
|
var arrayObj = BsonDocument.Parse(str);
|
||||||
|
return arrayObj;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var arrayObj = BsonArray.Create(json);
|
||||||
|
return arrayObj;
|
||||||
|
}
|
||||||
|
}
|
||||||
public static BsonValue MyCreate(object value)
|
public static BsonValue MyCreate(object value)
|
||||||
{
|
{
|
||||||
if (value is DateTime dt)
|
if (value is DateTime dt)
|
||||||
|
Loading…
Reference in New Issue
Block a user