Update mongodb

This commit is contained in:
sunkaixuan
2025-07-04 19:37:43 +08:00
parent f732cc36ae
commit c10950e7b6
4 changed files with 26 additions and 1 deletions

View File

@@ -198,6 +198,13 @@ namespace MongoDbTest
var list25 = db.Queryable<Student>().Where(it => it.Name == it.Name && it.Name=="a").ToList(); var list25 = db.Queryable<Student>().Where(it => it.Name == it.Name && it.Name=="a").ToList();
if (list25.Count!=1||list25.First().Name != "a") Cases.ThrowUnitError(); if (list25.Count!=1||list25.First().Name != "a") Cases.ThrowUnitError();
var list26 = db.Queryable<Student>().Select(it => new
{
Age1 = it.Age > 0? it.Age: 1,
Age2 =SqlFunc.IIF( it.Age > 0 , it.Age ,1),
}).ToList();
if (list26.First().Age1 != list26.First().Age2) Cases.ThrowUnitError();
} }
[SqlSugar.SugarTable("UnitStudent1231sds3z1")] [SqlSugar.SugarTable("UnitStudent1231sds3z1")]
public class Student : MongoDbBase public class Student : MongoDbBase

View File

@@ -60,6 +60,10 @@ namespace SqlSugar.MongoDb
{ {
projectionDocument[fieldName] = json; projectionDocument[fieldName] = json;
} }
else if (memberAssignment.Expression is MethodCallExpression callExpression&&callExpression.Method.Name==nameof(SqlFunc.IIF))
{
projectionDocument[fieldName] = json;
}
else else
{ {
projectionDocument[fieldName] = "$" + json.ToString(); projectionDocument[fieldName] = "$" + json.ToString();

View File

@@ -45,7 +45,7 @@ namespace SqlSugar.MongoDb
var funcString = context.ToString(model); var funcString = context.ToString(model);
result = BsonDocument.Parse(funcString); result = BsonDocument.Parse(funcString);
} }
else if (name.StartsWith("To")) else if (name.StartsWith("To")||name==nameof(SqlFunc.IIF))
{ {
var value = context.GetType().GetMethod(name).Invoke(context, new object[] { model }); var value = context.GetType().GetMethod(name).Invoke(context, new object[] { model });
result = BsonDocument.Parse(value?.ToString()); result = BsonDocument.Parse(value?.ToString());

View File

@@ -662,6 +662,20 @@ namespace SqlSugar.MongoDb
return inDoc.ToJson(UtilMethods.GetJsonWriterSettings()); return inDoc.ToJson(UtilMethods.GetJsonWriterSettings());
} }
public override string IIF(MethodCallExpressionModel model)
{
var test = model.Args[0].MemberValue as Expression;
var ifTrue = model.Args[1].MemberValue as Expression;
var ifFalse = model.Args[2].MemberValue as Expression;
// 构造 ConditionalExpression
var conditionalExpr = Expression.Condition(test, ifTrue, ifFalse);
BsonValue testValue = new ConditionalExpressionTractor(context, null).Extract(conditionalExpr);
return testValue.ToJson(UtilMethods.GetJsonWriterSettings());
}
#region Helper #region Helper
private static BsonValue GetMemberName(BsonValue memberName) private static BsonValue GetMemberName(BsonValue memberName)
{ {