mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-20 02:29:39 +08:00
Update mongodb
This commit is contained in:
@@ -48,6 +48,10 @@ namespace MongoDbTest
|
|||||||
cityName=city.Name
|
cityName=city.Name
|
||||||
}).ToList();
|
}).ToList();
|
||||||
if (dt.First().schoolName != "清华大学" || dt.First().studentName != "张三" || dt.First().cityName != "北京") Cases.ThrowUnitError();
|
if (dt.First().schoolName != "清华大学" || dt.First().studentName != "张三" || dt.First().cityName != "北京") Cases.ThrowUnitError();
|
||||||
|
|
||||||
|
var dt2 = db.Queryable<Student>()
|
||||||
|
.LeftJoin<School>((s, sc) => s.SchoolId == sc.Id)
|
||||||
|
.Select((s, sc) => sc).ToList();
|
||||||
}
|
}
|
||||||
[SqlSugar.SugarTable("UnitStudentdu2s31")]
|
[SqlSugar.SugarTable("UnitStudentdu2s31")]
|
||||||
public class Student : MongoDbBase
|
public class Student : MongoDbBase
|
||||||
|
@@ -56,6 +56,8 @@ namespace SqlSugar.MongoDb
|
|||||||
return new NewExpressionTractor(context, visitorContext).Extract(newNewExpression);
|
return new NewExpressionTractor(context, visitorContext).Extract(newNewExpression);
|
||||||
case ConditionalExpression conditionalExpression:
|
case ConditionalExpression conditionalExpression:
|
||||||
return new ConditionalExpressionTractor(context, visitorContext).Extract(conditionalExpression);
|
return new ConditionalExpressionTractor(context, visitorContext).Extract(conditionalExpression);
|
||||||
|
case ParameterExpression parameterExpression:
|
||||||
|
return new ParameterExpressionTractor(context, visitorContext).Extract(parameterExpression);
|
||||||
default:
|
default:
|
||||||
throw new NotSupportedException($"Unsupported expression: {expr.NodeType}");
|
throw new NotSupportedException($"Unsupported expression: {expr.NodeType}");
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,55 @@
|
|||||||
|
using MongoDB.Bson;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Linq;
|
||||||
|
namespace SqlSugar.MongoDb
|
||||||
|
{
|
||||||
|
public class ParameterExpressionTractor
|
||||||
|
{
|
||||||
|
private MongoNestedTranslatorContext context;
|
||||||
|
private ExpressionVisitorContext visitorContext;
|
||||||
|
|
||||||
|
public ParameterExpressionTractor(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
|
||||||
|
{
|
||||||
|
this.context = context;
|
||||||
|
this.visitorContext = visitorContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal BsonValue Extract(ParameterExpression parameterExpression)
|
||||||
|
{
|
||||||
|
BsonValue bson = new BsonDocument();
|
||||||
|
if (IsSelect())
|
||||||
|
{
|
||||||
|
var isJoinTable = this.context?.queryBuilder?.JoinQueryInfos?.Any(s => s.ShortName.EqualCase(parameterExpression.Name)) == true;
|
||||||
|
foreach (var item in this.context.context.EntityMaintenance.GetEntityInfo(parameterExpression.Type).Columns)
|
||||||
|
{
|
||||||
|
// 跳过忽略的列
|
||||||
|
if (item.IsIgnore) continue;
|
||||||
|
|
||||||
|
if (isJoinTable)
|
||||||
|
{
|
||||||
|
// 构建 "$x.y"
|
||||||
|
string mongoFieldPath = "$" + parameterExpression.Name + "." + item.DbColumnName;
|
||||||
|
|
||||||
|
// 设置别名
|
||||||
|
string alias = item.DbColumnName; // 或者你自己定义别名逻辑
|
||||||
|
|
||||||
|
((BsonDocument)bson).Add(alias, mongoFieldPath);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
((BsonDocument)bson).Add(item.DbColumnName, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bson;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsSelect()
|
||||||
|
{
|
||||||
|
return this.context.resolveType.IsIn(ResolveExpressType.SelectSingle, ResolveExpressType.SelectMultiple);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user