mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-23 20:43:46 +08:00
Update mongodb
This commit is contained in:
@@ -50,6 +50,12 @@ namespace MongoDbTest
|
||||
.Where(it => it.Id == ids.Last())
|
||||
.ExecuteCommand();
|
||||
|
||||
var updateRow5 = db.Updateable<OrderInfo>()
|
||||
.SetColumns(it =>new OrderInfo{ Name="aa", CreateTime = DateTime.Now})
|
||||
.Where(it => it.Id == ids.Last())
|
||||
.ExecuteCommand();
|
||||
|
||||
|
||||
var delrow = db.Deleteable(new OrderInfo() { Id = ids.Last() })
|
||||
.ExecuteCommand();
|
||||
|
||||
|
@@ -42,10 +42,17 @@ namespace SqlSugar.MongoDbCore
|
||||
return this.Visit(lambda.Body);
|
||||
case MethodCallExpression call:
|
||||
return new MethodCallExpressionTractor(context, visitorContext).Extract(call);
|
||||
case MemberInitExpression newMemberExp:
|
||||
return new MemberInitExpressionTractor(context, visitorContext).Extract(newMemberExp);
|
||||
default:
|
||||
throw new NotSupportedException($"Unsupported expression: {expr.NodeType}");
|
||||
}
|
||||
}
|
||||
|
||||
private object MemberInitExpressionTractor(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,45 @@
|
||||
using MongoDB.Bson;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar.MongoDbCore
|
||||
{
|
||||
public class MemberInitExpressionTractor
|
||||
{
|
||||
MongoNestedTranslatorContext _context;
|
||||
ExpressionVisitorContext _visitorContext;
|
||||
|
||||
public MemberInitExpressionTractor(MongoNestedTranslatorContext context, ExpressionVisitorContext visitorContext)
|
||||
{
|
||||
_context = context;
|
||||
_visitorContext = visitorContext;
|
||||
}
|
||||
|
||||
public BsonValue Extract(Expression expr)
|
||||
{
|
||||
if (this._context.resolveType == ResolveExpressType.Update)
|
||||
{
|
||||
return Update(expr);
|
||||
}
|
||||
throw new Exception("");
|
||||
}
|
||||
|
||||
private static BsonValue Update(Expression expr)
|
||||
{
|
||||
var exp = expr as MemberInitExpression;
|
||||
var setDocument = new BsonDocument();
|
||||
foreach (var binding in exp.Bindings)
|
||||
{
|
||||
if (binding is MemberAssignment assignment)
|
||||
{
|
||||
var fieldName = assignment.Member.Name;
|
||||
var fieldValue = Expression.Lambda(assignment.Expression).Compile().DynamicInvoke();
|
||||
setDocument[fieldName] = BsonValue.Create(fieldValue);
|
||||
}
|
||||
}
|
||||
return new BsonDocument("$set", setDocument);
|
||||
}
|
||||
}
|
||||
}
|
@@ -50,7 +50,13 @@ namespace SqlSugar.MongoDb
|
||||
{
|
||||
var operations = new List<string>();
|
||||
List<string> pks = this.PrimaryKeys;
|
||||
if (this.SetValues.Any())
|
||||
if (this.SetValues.Any() && this.SetValues.Any(it => it.Key == "$set"))
|
||||
{
|
||||
BsonArray filterArray = GetFilterArray();
|
||||
var filter = new BsonDocument("$and", filterArray);
|
||||
operations.Add($"{{ filter: {filter.ToJson(UtilMethods.GetJsonWriterSettings())} , update: {SetValues.FirstOrDefault().Value }}}");
|
||||
}
|
||||
else if (this.SetValues.Any())
|
||||
{
|
||||
var setOperation = new BsonDocument();
|
||||
foreach (var item in this.SetValues)
|
||||
@@ -61,12 +67,7 @@ namespace SqlSugar.MongoDb
|
||||
setOperation[element.Name] = element.Value;
|
||||
}
|
||||
}
|
||||
var filterArray = new BsonArray();
|
||||
foreach (var item in this.WhereValues)
|
||||
{
|
||||
var bson = BsonDocument.Parse(item); // 直接解析 JSON 为 BsonDocument
|
||||
filterArray.Add(bson); // 将每个条件添加到数组
|
||||
}
|
||||
var filterArray = GetFilterArray();
|
||||
var filter = new BsonDocument("$and", filterArray);
|
||||
operations.Add($"{{ filter: {filter.ToJson(UtilMethods.GetJsonWriterSettings())} , update: {{ $set: {setOperation.ToJson(UtilMethods.GetJsonWriterSettings())} }} }}");
|
||||
}
|
||||
@@ -82,6 +83,18 @@ namespace SqlSugar.MongoDb
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private BsonArray GetFilterArray()
|
||||
{
|
||||
var filterArray = new BsonArray();
|
||||
foreach (var item in this.WhereValues)
|
||||
{
|
||||
var bson = BsonDocument.Parse(item); // 直接解析 JSON 为 BsonDocument
|
||||
filterArray.Add(bson); // 将每个条件添加到数组
|
||||
}
|
||||
|
||||
return filterArray;
|
||||
}
|
||||
|
||||
private static void UpdateByObject(List<IGrouping<int, DbColumnInfo>> groupList, List<string> operations, List<string> pks)
|
||||
{
|
||||
foreach (var group in groupList)
|
||||
|
@@ -28,5 +28,14 @@ namespace SqlSugar.MongoDb
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public override IUpdateable<T> SetColumns(Expression<Func<T, T>> columns)
|
||||
{
|
||||
ThrowUpdateByObject();
|
||||
var expResult = UpdateBuilder.GetExpressionValue(columns, ResolveExpressType.Update)
|
||||
.GetResultString();
|
||||
this.UpdateBuilder.SetValues.Add(new KeyValuePair<string, string>("$set", expResult));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user