Update mongodb

This commit is contained in:
sunkaixuan
2025-07-29 08:53:23 +08:00
parent 1147b6988f
commit 0502643208
3 changed files with 21 additions and 2 deletions

View File

@@ -234,11 +234,16 @@ namespace SqlSugar.MongoDb
else if (col.Value != null && col.DataType == nameof(ObjectId))
{
doc[col.DbColumnName] = UtilMethods.MyCreate(ObjectId.Parse(col.Value?.ToString()));
}
}
else if (col.InsertServerTime)
{
doc[col.DbColumnName] = UtilMethods.MyCreate(DateTime.Now);
}
else if (col.SqlParameterDbType is Type t&& typeof(ISugarDataConverter).IsAssignableFrom(t))
{
var p =UtilMethods.GetParameterConverter(0,this.Context,col.Value,this.EntityInfo,this.EntityInfo.Columns.FirstOrDefault(s=>s.PropertyName.EqualCase(col.PropertyName)));
doc[col.DbColumnName] = UtilMethods.MyCreate(p.Value);
}
else
{
doc[col.DbColumnName] = UtilMethods.MyCreate(col.Value);

View File

@@ -95,7 +95,7 @@ namespace SqlSugar.MongoDb
return filterArray;
}
private static void UpdateByObject(List<IGrouping<int, DbColumnInfo>> groupList, List<string> operations, List<string> pks)
private void UpdateByObject(List<IGrouping<int, DbColumnInfo>> groupList, List<string> operations, List<string> pks)
{
foreach (var group in groupList)
{
@@ -122,6 +122,11 @@ namespace SqlSugar.MongoDb
var bsonValue = UtilMethods.MyCreate(col.Value, col);
setDoc[col.DbColumnName] = bsonValue;
}
else if (col.SqlParameterDbType is Type t && typeof(ISugarDataConverter).IsAssignableFrom(t))
{
var value = UtilMethods.GetParameterConverter(0, this.Context, col.Value, this.EntityInfo, this.EntityInfo.Columns.FirstOrDefault(s => s.PropertyName.EqualCase(col.PropertyName)));
setDoc[col.DbColumnName] = UtilMethods.MyCreate(value);
}
}
var update = new BsonDocument

View File

@@ -22,6 +22,15 @@ namespace SqlSugar.MongoDb
{
public class UtilMethods
{
internal static SugarParameter GetParameterConverter(int index, ISqlSugarClient db, object value, EntityInfo entityInfo, EntityColumnInfo columnInfo)
{
var entity = entityInfo;
var type = columnInfo.SqlParameterDbType as Type;
var ParameterConverter = type.GetMethod("ParameterConverter").MakeGenericMethod(columnInfo.PropertyInfo.PropertyType);
var obj = Activator.CreateInstance(type);
var p = ParameterConverter.Invoke(obj, new object[] { value, 100 + index }) as SugarParameter;
return p;
}
public static bool IsCollectionOrArrayButNotByteArray(Type type)
{
if (type == null)