Update mongodb

This commit is contained in:
sunkaixuan 2025-06-23 19:19:15 +08:00
parent 8870fb173c
commit 155110908d
7 changed files with 110 additions and 22 deletions

View File

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

View File

@ -0,0 +1,48 @@
using SqlSugar.MongoDb;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MongoDbTest
{
public class QueryJson
{
internal static void Init()
{
var db = DBHelper.DbHelper.GetNewDb();
db.CodeFirst.InitTables<Student>();
db.DbMaintenance.TruncateTable<Student>();
db.Insertable(new Book() { Price = 1, CreateTime = DateTime.Now }).ExecuteCommand();
var data1=db.Queryable<Book>().Where(it => it.Price == 1).ToList();
db.Insertable(new Student() { Age = 1, Name = "tom", SchoolId = "a", Book = new Book() { CreateTime = DateTime.Now, Price = 1 } }).ExecuteCommand();
db.Insertable(new Student() { Age = 1, Name = "tom2", SchoolId = "a2", Book = new Book() { CreateTime = DateTime.Now, Price = 2 } }).ExecuteCommand();
var data2 = db.Queryable<Student>().Where(it => it.Book.Price == 1).ToList();
if (data2.Count != 1) Cases.ThrowUnitError();
if (data2.First().Book.Price != 1) 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 Book Book { get; set; }
}
public class Book
{
public decimal Price { get; set; }
public DateTime CreateTime { get; set; }
}
}
}

View File

@ -43,10 +43,7 @@ namespace SqlSugar.MongoDb
{ "_id", new BsonDocument { { "$in", bsonArray } } }
};
string json = filter.ToJson(new MongoDB.Bson.IO.JsonWriterSettings
{
OutputMode = MongoDB.Bson.IO.JsonOutputMode.Shell
}); // 使用 MongoDB 驱动的序列化
string json = filter.ToJson(UtilMethods.GetJsonWriterSettings()); // 使用 MongoDB 驱动的序列化
jsonObjects.Add(json);
}
}

View File

@ -17,10 +17,7 @@ namespace SqlSugar.MongoDb
context.context = this.SugarContext.Context;
context.queryBuilder = this.SugarContext.QueryBuilder;
var sql=MongoNestedTranslator.Translate(expression, context);
var shellString = sql.ToJson(new JsonWriterSettings
{
OutputMode = JsonOutputMode.Shell
});
var shellString = sql.ToJson(UtilMethods.GetJsonWriterSettings());
this.Result.Append(shellString);
}

View File

@ -1,5 +1,9 @@
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using NetTaste;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
@ -10,6 +14,49 @@ namespace SqlSugar.MongoDb
{
public class MongoDbInsertBuilder : InsertBuilder
{
public MongoDbInsertBuilder()
{
this.SerializeObjectFunc = it =>
{
object value =it;
if (value is IEnumerable enumerable)
{
var realType = value.GetType();
var bson = value.ToBson(realType);
var json = bson.ToJson(UtilMethods.GetJsonWriterSettings());
return json;
}
else
{
var realType = it.GetType();
var bson = it.ToBson(realType); // → byte[]
var doc = BsonSerializer.Deserialize<BsonDocument>(bson); // → BsonDocument
var json = doc.ToJson(UtilMethods.GetJsonWriterSettings());
return json;
}
};
this.DeserializeObjectFunc = (json, type) =>
{
if (json is Dictionary<string, object> keyValues)
{
// 先用 Dictionary 构建 BsonDocument
var bsonDoc = new BsonDocument();
foreach (var kvp in keyValues)
{
bsonDoc.Add(kvp.Key, BsonValue.Create(kvp.Value));
}
// 再用 BsonSerializer 反序列化为 T
return BsonSerializer.Deserialize(bsonDoc, type);
}
else
{
return null;
}
};
}
public override string SqlTemplate
{
get
@ -59,14 +106,18 @@ namespace SqlSugar.MongoDb
foreach (var col in group)
{
// 自动推断类型,如 string、int、bool、DateTime、ObjectId 等
if (col.IsJson == true)
{
doc[col.DbColumnName] = BsonDocument.Parse(col.Value?.ToString());
}
else
{
doc[col.DbColumnName] = UtilMethods.MyCreate(col.Value);
}
}
// 转为 JSON 字符串(标准 MongoDB shell 格式)
string json = doc.ToJson(new MongoDB.Bson.IO.JsonWriterSettings
{
OutputMode = MongoDB.Bson.IO.JsonOutputMode.Shell // 可改成 Strict
});
string json = doc.ToJson(UtilMethods.GetJsonWriterSettings());
jsonObjects.Add(json);
}

View File

@ -137,10 +137,7 @@ namespace SqlSugar.MongoDb
{ "update", update }
};
string json = entry.ToJson(new MongoDB.Bson.IO.JsonWriterSettings
{
OutputMode = MongoDB.Bson.IO.JsonOutputMode.Shell // JSON标准格式带双引号
});
string json = entry.ToJson(UtilMethods.GetJsonWriterSettings());
operations.Add(json);
}

View File

@ -28,10 +28,7 @@ namespace SqlSugar.MongoDb
}
internal static MongoDB.Bson.IO.JsonWriterSettings GetJsonWriterSettings()
{
return new MongoDB.Bson.IO.JsonWriterSettings
{
OutputMode = MongoDB.Bson.IO.JsonOutputMode.Shell // JSON标准格式带双引号
};
return new MongoDB.Bson.IO.JsonWriterSettings { };
}
internal static DateTime GetMinDate(ConnectionConfig currentConnectionConfig)
{