Update mongodb

This commit is contained in:
sunkaixuan 2025-06-13 21:00:09 +08:00
parent a5a1ddf47a
commit 931a5b5a6c
11 changed files with 32 additions and 24 deletions

View File

@ -55,7 +55,9 @@ namespace MongoDbTest
var list6 = db.Queryable<Student>().Where(it => it.CreateDateTime == DateTime.Now.AddDays(1)).ToList();
var dt = DateTime.Now.AddDays(-10).Date;
var list7 = db.Queryable<Student>().Where(it => it.CreateDateTime.Date == dt).ToList();
var list70=db.Queryable<Student>().ToList().Where(it => it.CreateDateTime.Date == dt).ToList();
var list71 = db.Queryable<Student>().Where(it => it.CreateDateTime.Date == dt).ToList();
if (list71.Count != list70.Count) Cases.ThrowUnitError();
}
private static void ValidateStudentData(SqlSugar.SqlSugarClient db)
@ -69,8 +71,10 @@ namespace MongoDbTest
{
db.CodeFirst.InitTables<Student>();
db.DbMaintenance.TruncateTable<Student>();
db.Insertable(new Student() { Name = "jack",CreateDateTime=DateTime.Now.AddDays(-10), Bool = true, SchoolId = 2 }).ExecuteCommand();
var dt = DateTime.Now.AddDays(-10);
db.Insertable(new Student() { Name = "jack",CreateDateTime= dt, Bool = true, SchoolId = 2 }).ExecuteCommand();
db.Insertable(new Student() { Name = "tom_null", CreateDateTime = DateTime.Now.AddDays(-110), Bool = false, BoolNull = true, SchoolId = 3, SchoolIdNull = 4 }).ExecuteCommand();
var x=db.Queryable<Student>().ToList();
}
[SqlSugar.SugarTable("UnitStudent1ssss23s131")]

View File

@ -49,12 +49,6 @@ namespace SqlSugar.MongoDb
default:
throw new NotSupportedException($"Unsupported expression: {expr.NodeType}");
}
}
private object MemberInitExpressionTractor(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
{
throw new NotImplementedException();
}
}
}
}
}

View File

@ -26,7 +26,7 @@ namespace SqlSugar.MongoDb
if (ExpressionTool.GetParameters(expr).Count == 0)
{
var value = ExpressionTool.GetMemberValue(oldMember.Member, oldExp);
return BsonValue.Create(value);
return UtilMethods.MyCreate(value);
}
else if (!string.IsNullOrEmpty(BinaryExpressionTranslator.GetSystemDateMemberName(expr)))
{
@ -36,7 +36,7 @@ namespace SqlSugar.MongoDb
if (memberExp.Member.Name == "Date")
{
model.Args.Add(new MethodCallExpressionArgs() { MemberValue = memberExp.Expression });
return method.ToDateShort(model);
return BsonDocument.Parse(method.ToDateShort(model));
}
}
@ -65,11 +65,11 @@ namespace SqlSugar.MongoDb
var columnInfo = entityInfo.Columns.FirstOrDefault(s => s.PropertyName == parts.First());
if (columnInfo != null)
{
return BsonValue.Create(columnInfo.DbColumnName);
return UtilMethods.MyCreate(columnInfo.DbColumnName);
}
}
}
return BsonValue.Create(string.Join(".", parts));
return UtilMethods.MyCreate(string.Join(".", parts));
}
}

View File

@ -74,7 +74,7 @@ namespace SqlSugar.MongoDb
}
else
{
setDocument[fieldName] = BsonValue.Create(fieldValue);
setDocument[fieldName] = UtilMethods.MyCreate(fieldValue);
}
}
}

View File

@ -19,7 +19,7 @@ namespace SqlSugar.MongoDb
{
if (ExpressionTool.GetParameters(expr).Count == 0)
{
return BsonValue.Create(ExpressionTool.DynamicInvoke(expr));
return UtilMethods.MyCreate(ExpressionTool.DynamicInvoke(expr));
}
else
{
@ -38,7 +38,7 @@ namespace SqlSugar.MongoDb
model.Args.Add(new MethodCallExpressionArgs() { MemberValue = item });
}
var funcString= context.GetType().GetMethod(name).Invoke(context, new object[] { model });
result = BsonValue.Create(funcString);
result = UtilMethods.MyCreate(funcString);
}
return result;
}

View File

@ -80,7 +80,7 @@ namespace SqlSugar.MongoDb
}
else
{
setDocument[fieldName] = BsonValue.Create(fieldValue);
setDocument[fieldName] = UtilMethods.MyCreate(fieldValue);
}
}

View File

@ -18,7 +18,7 @@ namespace SqlSugar.MongoDb
public BsonValue Extract(ConstantExpression expr)
{
return BsonValue.Create(expr.Value);
return UtilMethods.MyCreate(expr.Value);
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Text;

View File

@ -59,7 +59,7 @@ namespace SqlSugar.MongoDb
foreach (var col in group)
{
// 自动推断类型,如 string、int、bool、DateTime、ObjectId 等
doc[col.DbColumnName] = BsonValue.Create(col.Value);
doc[col.DbColumnName] = UtilMethods.MyCreate(col.Value);
}
// 转为 JSON 字符串(标准 MongoDB shell 格式)

View File

@ -107,11 +107,11 @@ namespace SqlSugar.MongoDb
if (col.IsPrimarykey || pks.Contains(col.DbColumnName))
{
filter[col.DbColumnName] = BsonValue.Create(ObjectId.Parse(col.Value?.ToString())); ;
filter[col.DbColumnName] = UtilMethods.MyCreate(ObjectId.Parse(col.Value?.ToString())); ;
}
else
{
var bsonValue = BsonValue.Create(col.Value);
var bsonValue = UtilMethods.MyCreate(col.Value);
setDoc[col.DbColumnName] = bsonValue;
}
}

View File

@ -16,7 +16,16 @@ using System.Text.RegularExpressions;
namespace SqlSugar.MongoDb
{
public class UtilMethods
{
{
public static BsonValue MyCreate(object value)
{
if (value is DateTime dt)
{
var utcNow = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
return new BsonDateTime(utcNow);
}
return BsonValue.Create(value);
}
internal static MongoDB.Bson.IO.JsonWriterSettings GetJsonWriterSettings()
{
return new MongoDB.Bson.IO.JsonWriterSettings