Update mongodb

This commit is contained in:
sunkaixuan 2025-06-26 14:33:33 +08:00
parent 48c3e0e522
commit c3e1d64cbd
2 changed files with 23 additions and 29 deletions

View File

@ -21,11 +21,16 @@ namespace MongoDbTest
var student = new Student { Name = "TestStudent", SchoolId = ids.Last() }; var student = new Student { Name = "TestStudent", SchoolId = ids.Last() };
db.Insertable(student).ExecuteCommand(); db.Insertable(student).ExecuteCommand();
// 添加学生数据SchoolId 关联学校并且没有学校 // 添加学生数据SchoolId 关联学校并且没有学校
var student2 = new Student { Name = "TestStudent", SchoolId = var student2 = new Student { Name = "TestStudentNoSchool", SchoolId =
ObjectId.GenerateNewId().ToString() ObjectId.GenerateNewId().ToString()
}; };
db.Insertable(student2).ExecuteCommand(); db.Insertable(student2).ExecuteCommand();
if (db.Queryable<Student>().First(it => it.SchoolId == ids.Last()).Name != "TestStudent") Cases.ThrowUnitError();
db.Updateable(db.Queryable<Student>().First(it => it.SchoolId == ids.Last())).ExecuteCommand();
if (db.Queryable<Student>().First(it => it.SchoolId == ids.Last()).Name != "TestStudent") Cases.ThrowUnitError();
//var list=db.Queryable<Student>() //var list=db.Queryable<Student>()
// .LeftJoin<School>((x, y) => x.SchoolId == y.Id) // .LeftJoin<School>((x, y) => x.SchoolId == y.Id)
// .Where((x,y)=>x.Name =="a"||y.Name=="a") // .Where((x,y)=>x.Name =="a"||y.Name=="a")
@ -38,7 +43,7 @@ namespace MongoDbTest
public class Student : MongoDbBase public class Student : MongoDbBase
{ {
public string Name { get; set; } public string Name { get; set; }
[SqlSugar.SugarColumn(ColumnDataType =nameof(ObjectId))]
public string SchoolId { get; set; } public string SchoolId { get; set; }
} }
[SqlSugar.SugarTable("UnitSchool123131")] [SqlSugar.SugarTable("UnitSchool123131")]

View File

@ -38,12 +38,12 @@ namespace SqlSugar.MongoDb
} }
protected override string ToSingleSqlString(List<IGrouping<int, DbColumnInfo>> groupList) protected override string ToSingleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
{ {
var result = BuildUpdateMany(groupList, this.TableName); var result = BuildUpdateMany(groupList, this.TableName);
return result; return result;
} }
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList) protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
{ {
var result = BuildUpdateMany(groupList,this.TableName); var result = BuildUpdateMany(groupList, this.TableName);
return result; return result;
} }
public string BuildUpdateMany(List<IGrouping<int, DbColumnInfo>> groupList, string tableName) public string BuildUpdateMany(List<IGrouping<int, DbColumnInfo>> groupList, string tableName)
@ -54,7 +54,7 @@ namespace SqlSugar.MongoDb
{ {
BsonArray filterArray = GetFilterArray(); BsonArray filterArray = GetFilterArray();
var filter = new BsonDocument("$and", filterArray); 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()) else if (this.SetValues.Any())
{ {
@ -105,38 +105,27 @@ namespace SqlSugar.MongoDb
foreach (var col in group) 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); 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 else
{ {
var bsonValue = UtilMethods.MyCreate(col.Value); filter[col.DbColumnName] = UtilMethods.MyCreate(ObjectId.Parse(col.Value?.ToString()));
setDoc[col.DbColumnName] = bsonValue;
} }
} }
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 var update = new BsonDocument