From c3e1d64cbd3bf42b536bd68bf745216208ede1bb Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Thu, 26 Jun 2025 14:33:33 +0800 Subject: [PATCH] Update mongodb --- .../MongoDbTest/UnitTest/QueryLeftJoin.cs | 9 +++- .../SqlBuilder/MongoDbUpdateBuilder.cs | 43 +++++++------------ 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryLeftJoin.cs b/Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryLeftJoin.cs index c2429190a..cf6236739 100644 --- a/Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryLeftJoin.cs +++ b/Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryLeftJoin.cs @@ -21,11 +21,16 @@ namespace MongoDbTest var student = new Student { Name = "TestStudent", SchoolId = ids.Last() }; db.Insertable(student).ExecuteCommand(); // 添加学生数据,SchoolId 关联学校并且没有学校 - var student2 = new Student { Name = "TestStudent", SchoolId = + var student2 = new Student { Name = "TestStudentNoSchool", SchoolId = ObjectId.GenerateNewId().ToString() }; db.Insertable(student2).ExecuteCommand(); + + if (db.Queryable().First(it => it.SchoolId == ids.Last()).Name != "TestStudent") Cases.ThrowUnitError(); + db.Updateable(db.Queryable().First(it => it.SchoolId == ids.Last())).ExecuteCommand(); + if (db.Queryable().First(it => it.SchoolId == ids.Last()).Name != "TestStudent") Cases.ThrowUnitError(); + //var list=db.Queryable() // .LeftJoin((x, y) => x.SchoolId == y.Id) // .Where((x,y)=>x.Name =="a"||y.Name=="a") @@ -38,7 +43,7 @@ namespace MongoDbTest public class Student : MongoDbBase { public string Name { get; set; } - + [SqlSugar.SugarColumn(ColumnDataType =nameof(ObjectId))] public string SchoolId { get; set; } } [SqlSugar.SugarTable("UnitSchool123131")] diff --git a/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/SqlBuilder/MongoDbUpdateBuilder.cs b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/SqlBuilder/MongoDbUpdateBuilder.cs index 1047c6225..6cdddc64d 100644 --- a/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/SqlBuilder/MongoDbUpdateBuilder.cs +++ b/Src/Asp.NetCore2/SqlSugar.MongoDbCore/MongoDb/SqlBuilder/MongoDbUpdateBuilder.cs @@ -38,12 +38,12 @@ namespace SqlSugar.MongoDb } protected override string ToSingleSqlString(List> groupList) { - var result = BuildUpdateMany(groupList, this.TableName); + var result = BuildUpdateMany(groupList, this.TableName); return result; } protected override string TomultipleSqlString(List> groupList) { - var result = BuildUpdateMany(groupList,this.TableName); + var result = BuildUpdateMany(groupList, this.TableName); return result; } public string BuildUpdateMany(List> groupList, string tableName) @@ -54,7 +54,7 @@ namespace SqlSugar.MongoDb { BsonArray filterArray = GetFilterArray(); var filter = new BsonDocument("$and", filterArray); - operations.Add($"{{ filter: {filter.ToJson(UtilMethods.GetJsonWriterSettings())} , update: {SetValues.FirstOrDefault().Value }}}"); + operations.Add($"{{ filter: {filter.ToJson(UtilMethods.GetJsonWriterSettings())} , update: {SetValues.FirstOrDefault().Value}}}"); } else if (this.SetValues.Any()) { @@ -105,38 +105,27 @@ namespace SqlSugar.MongoDb foreach (var col in group) { - if (col.IsPrimarykey || pks.Contains(col.DbColumnName)) + if (col.DbColumnName.EqualCase("_id") || col.DataType == nameof(ObjectId)) { - if (col.Value == null) + if (col.Value == null) { filter[col.DbColumnName] = UtilMethods.MyCreate(col.Value); } - else if(col.DbColumnName.EqualCase("_id")) - { - filter[col.DbColumnName] = UtilMethods.MyCreate(ObjectId.Parse(col.Value?.ToString())); - } - else if (col.DataType == nameof(ObjectId)) - { - filter[col.DbColumnName] = UtilMethods.MyCreate(ObjectId.Parse(col.Value?.ToString())); - } - else - { - filter[col.DbColumnName] = UtilMethods.MyCreate(col.Value); - } - } - else - { - if (col.IsJson) - { - var bsonValue = UtilMethods.ParseJsonObject(col.Value?.ToString()); - setDoc[col.DbColumnName] = bsonValue; - } else { - var bsonValue = UtilMethods.MyCreate(col.Value); - setDoc[col.DbColumnName] = bsonValue; + filter[col.DbColumnName] = UtilMethods.MyCreate(ObjectId.Parse(col.Value?.ToString())); } } + else if (col.IsJson) + { + var bsonValue = UtilMethods.ParseJsonObject(col.Value?.ToString()); + setDoc[col.DbColumnName] = bsonValue; + } + else + { + var bsonValue = UtilMethods.MyCreate(col.Value); + setDoc[col.DbColumnName] = bsonValue; + } } var update = new BsonDocument