mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-22 12:09:29 +08:00
Update mongodb
This commit is contained in:
@@ -103,6 +103,13 @@ namespace MongoDbTest
|
||||
Name=it.Name
|
||||
}).ToDataTable();
|
||||
|
||||
var list11 = db.Queryable<OrderInfo>()
|
||||
.Select(it => new
|
||||
{
|
||||
Id = it.Id,
|
||||
Name = it.Name
|
||||
}).ToDataTable();
|
||||
|
||||
//测试生成SQL性能
|
||||
TestSqlBuilder(db);
|
||||
}
|
||||
|
@@ -44,6 +44,8 @@ namespace SqlSugar.MongoDbCore
|
||||
return new MethodCallExpressionTractor(context, visitorContext).Extract(call);
|
||||
case MemberInitExpression newMemberExp:
|
||||
return new MemberInitExpressionTractor(context, visitorContext).Extract(newMemberExp);
|
||||
case NewExpression newNewExpression:
|
||||
return new NewExpressionTractor(context, visitorContext).Extract(newNewExpression);
|
||||
default:
|
||||
throw new NotSupportedException($"Unsupported expression: {expr.NodeType}");
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
@@ -39,6 +40,18 @@ namespace SqlSugar.MongoDbCore
|
||||
{
|
||||
_visitorContext.ExpType = typeof(MemberExpression);
|
||||
}
|
||||
if (parts.Count == 1&& expr is ParameterExpression parameter)
|
||||
{
|
||||
if (_context?.context != null)
|
||||
{
|
||||
var entityInfo = _context.context.EntityMaintenance.GetEntityInfo(parameter.Type);
|
||||
var columnInfo = entityInfo.Columns.FirstOrDefault(s => s.PropertyName == parts.First());
|
||||
if (columnInfo != null)
|
||||
{
|
||||
return BsonValue.Create(columnInfo.DbColumnName);
|
||||
}
|
||||
}
|
||||
}
|
||||
return BsonValue.Create(string.Join(".", parts));
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using MongoDB.Bson;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
@@ -32,23 +33,28 @@ namespace SqlSugar.MongoDbCore
|
||||
|
||||
private BsonValue Select(Expression expr)
|
||||
{
|
||||
var exp = expr as MemberInitExpression;
|
||||
var exp = expr as MemberInitExpression ?? throw new InvalidOperationException("Expression must be NewExpression");
|
||||
var projectionDocument = new BsonDocument();
|
||||
|
||||
// Iterate over the bindings in the MemberInitExpression
|
||||
foreach (var binding in exp.Bindings)
|
||||
// Iterate over the bindings of the NewExpression
|
||||
foreach (MemberBinding binding in exp.Bindings)
|
||||
{
|
||||
if (binding is MemberAssignment assignment)
|
||||
if (binding.BindingType != MemberBindingType.Assignment)
|
||||
{
|
||||
var fieldName = assignment.Member.Name; // 原字段名
|
||||
|
||||
// 将原字段名动态转换为 新字段名(例如:name -> name1)
|
||||
var newFieldName = fieldName ;
|
||||
|
||||
// 将字段投影为 "新字段名" : "$原字段名"
|
||||
projectionDocument[newFieldName] = $"${fieldName}";
|
||||
throw new NotSupportedException("Only MemberAssignments are supported.");
|
||||
}
|
||||
}
|
||||
|
||||
// Cast to MemberAssignment to access the value of the field
|
||||
MemberAssignment memberAssignment = (MemberAssignment)binding;
|
||||
|
||||
// Extract the field name (the name of the property or field)
|
||||
var fieldName = binding.Member.Name;
|
||||
|
||||
// Build the projection document with the field name and its reference in MongoDB
|
||||
var json=new ExpressionVisitor(_context, _visitorContext).Visit(memberAssignment.Expression);
|
||||
projectionDocument[fieldName] = "$" + json.ToString();
|
||||
}
|
||||
projectionDocument["_id"] = 0;
|
||||
return projectionDocument;
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,78 @@
|
||||
using MongoDB.Bson;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace SqlSugar.MongoDbCore
|
||||
{
|
||||
public class NewExpressionTractor
|
||||
{
|
||||
MongoNestedTranslatorContext _context;
|
||||
ExpressionVisitorContext _visitorContext;
|
||||
|
||||
public NewExpressionTractor(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
|
||||
{
|
||||
_context = context;
|
||||
_visitorContext = visitorContext;
|
||||
}
|
||||
|
||||
public BsonValue Extract(Expression expr)
|
||||
{
|
||||
if (this._context.resolveType == ResolveExpressType.Update)
|
||||
{
|
||||
return Update(expr);
|
||||
}
|
||||
else if (this._context.resolveType == ResolveExpressType.SelectSingle)
|
||||
{
|
||||
return Select(expr);
|
||||
}
|
||||
throw new NotSupportedException(this._context.resolveType + "");
|
||||
}
|
||||
|
||||
private BsonValue Select(Expression expr)
|
||||
{
|
||||
|
||||
var exp = expr as NewExpression ?? throw new InvalidOperationException("Expression must be NewExpression");
|
||||
var projectionDocument = new BsonDocument();
|
||||
|
||||
// 遍历 NewExpression 的成员
|
||||
foreach (var member in exp.Members)
|
||||
{
|
||||
// 获取字段名
|
||||
var fieldName = member.Name;
|
||||
|
||||
// 使用 ExpressionVisitor 访问表达式
|
||||
var json = new ExpressionVisitor(_context, _visitorContext).Visit(exp.Arguments[exp.Members.IndexOf(member)]);
|
||||
|
||||
// 构建 MongoDB 的投影文档
|
||||
projectionDocument[fieldName] = "$" + json.ToString();
|
||||
}
|
||||
projectionDocument["_id"] = 0;
|
||||
return projectionDocument;
|
||||
}
|
||||
|
||||
private BsonValue Update(Expression expr)
|
||||
{
|
||||
var exp = expr as NewExpression ?? throw new InvalidOperationException("Expression must be NewExpression");
|
||||
var setDocument = new BsonDocument();
|
||||
|
||||
for (int i = 0; i < exp.Members.Count; i++)
|
||||
{
|
||||
var fieldName = exp.Members[i].Name;
|
||||
var assignmentExpr = exp.Arguments[i];
|
||||
var fieldValue = new ExpressionVisitor(_context, _visitorContext).Visit(Expression.Lambda(assignmentExpr));
|
||||
|
||||
if (assignmentExpr is MemberExpression && ExpressionTool.GetParameters(assignmentExpr).Count > 0)
|
||||
{
|
||||
setDocument[fieldName] = new BsonDocument("$getField", fieldValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
setDocument[fieldName] = BsonValue.Create(fieldValue);
|
||||
}
|
||||
}
|
||||
|
||||
return new BsonDocument("$set", setDocument);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user