Update mongodb

This commit is contained in:
sunkaixuan 2025-06-23 20:43:14 +08:00
parent 446af2afad
commit b8d1ae414d
4 changed files with 99 additions and 9 deletions

View File

@ -14,7 +14,8 @@ namespace MongoDbTest
QuerySingle.Init();
QueryWhere.Init();
QuerySelect.Init();
QueryJson.Init();
QueryJson.Init();
QueryJsonArray.Init();
Insert.Init();
Update.Init();
Delete.Init();

View 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; }
}
}
}

View File

@ -9,6 +9,7 @@ using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace SqlSugar.MongoDb
{
@ -22,10 +23,18 @@ namespace SqlSugar.MongoDb
if (value is IEnumerable enumerable)
{
var realType = value.GetType();
var bson = value.ToBson(realType);
var json = bson.ToJson(UtilMethods.GetJsonWriterSettings());
return json;
var list = new List<BsonDocument>();
foreach (var e in enumerable)
{
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
{
@ -51,9 +60,30 @@ namespace SqlSugar.MongoDb
// 再用 BsonSerializer 反序列化为 T
return BsonSerializer.Deserialize(bsonDoc, type);
}
else
else if (json is List<object> list)
{
return null;
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
{
return json;
}
};
}
@ -108,7 +138,7 @@ namespace SqlSugar.MongoDb
// 自动推断类型,如 string、int、bool、DateTime、ObjectId 等
if (col.IsJson == true)
{
doc[col.DbColumnName] = BsonDocument.Parse(col.Value?.ToString());
doc[col.DbColumnName] =UtilMethods.ParseJsonObject(col.Value);
}
else
{

View File

@ -1,4 +1,5 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@ -16,7 +17,20 @@ using System.Text.RegularExpressions;
namespace SqlSugar.MongoDb
{
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)
{
if (value is DateTime dt)