mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-22 12:09:29 +08:00
Update mongoDb
This commit is contained in:
@@ -97,6 +97,12 @@ namespace MongoDbTest
|
||||
|
||||
var list9 = db.Queryable<OrderInfo>().OrderBy(it=>it.Name).OrderByDescending(it => it.Price).ToList();
|
||||
|
||||
var list10 = db.Queryable<OrderInfo>()
|
||||
.Select(it=>new OrderInfo() {
|
||||
Id=it.Id,
|
||||
Name=it.Name
|
||||
}).ToDataTable();
|
||||
|
||||
//测试生成SQL性能
|
||||
TestSqlBuilder(db);
|
||||
}
|
||||
|
@@ -23,7 +23,33 @@ namespace SqlSugar.MongoDbCore
|
||||
{
|
||||
return Update(expr);
|
||||
}
|
||||
throw new Exception("");
|
||||
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 MemberInitExpression;
|
||||
var projectionDocument = new BsonDocument();
|
||||
|
||||
// Iterate over the bindings in the MemberInitExpression
|
||||
foreach (var binding in exp.Bindings)
|
||||
{
|
||||
if (binding is MemberAssignment assignment)
|
||||
{
|
||||
var fieldName = assignment.Member.Name; // 原字段名
|
||||
|
||||
// 将原字段名动态转换为 新字段名(例如:name -> name1)
|
||||
var newFieldName = fieldName ;
|
||||
|
||||
// 将字段投影为 "新字段名" : "$原字段名"
|
||||
projectionDocument[newFieldName] = $"${fieldName}";
|
||||
}
|
||||
}
|
||||
return projectionDocument;
|
||||
}
|
||||
|
||||
private BsonValue Update(Expression expr)
|
||||
|
@@ -1,7 +1,9 @@
|
||||
using MongoDB.Bson;
|
||||
using SqlSugar.MongoDbCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.RegularExpressions;
|
||||
@@ -113,6 +115,19 @@ namespace SqlSugar.MongoDb
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Select
|
||||
if (this.SelectValue is Expression expression)
|
||||
{
|
||||
var dos=MongoNestedTranslator.Translate(expression, new MongoNestedTranslatorContext() {
|
||||
context = this.Context,
|
||||
resolveType=ResolveExpressType.SelectSingle
|
||||
});
|
||||
var json = dos.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||
operations.Add($"{{\"$project\": {json} }}");
|
||||
}
|
||||
#endregion
|
||||
|
||||
sb.Append($"aggregate {this.GetTableNameString} ");
|
||||
sb.Append("[");
|
||||
sb.Append(string.Join(", ", operations));
|
||||
|
Reference in New Issue
Block a user