From 358e175799b0b677451225b4f2783de09f289f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=8C?= <259278618@qq.com> Date: Tue, 2 Dec 2025 15:56:57 +0800 Subject: [PATCH] =?UTF-8?q?mongo=20update=E6=96=B9=E6=B3=95=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mongo/mongo.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mongo/mongo.go b/mongo/mongo.go index 1318dfb..92f0c2a 100644 --- a/mongo/mongo.go +++ b/mongo/mongo.go @@ -99,7 +99,7 @@ func Delete(ctx context.Context, filter bson.M, collection string, opts ...optio } // Update 修改记录 -func Update(ctx context.Context, filter bson.M, update interface{}, collection string, opts ...options.Lister[options.UpdateManyOptions]) (result *mongo.UpdateResult, err error) { +func Update(ctx context.Context, filter bson.M, update bson.M, collection string, opts ...options.Lister[options.UpdateManyOptions]) (result *mongo.UpdateResult, err error) { if len(filter) == 0 { err = gerror.New("缺少查询条件") return @@ -110,10 +110,11 @@ func Update(ctx context.Context, filter bson.M, update interface{}, collection s return } filter["tenantId"] = user.TenantId - doc := gconv.Map(update) - doc["updater"] = user.UserName - doc["updatedAt"] = gtime.Now().Time - result, err = db.Collection(collection).UpdateMany(ctx, filter, doc, opts...) + setDoc := update["$set"].(bson.M) + setDoc["updater"] = user.UserName + setDoc["updatedAt"] = gtime.Now().Time + update = bson.M{"$set": setDoc} + result, err = db.Collection(collection).UpdateMany(ctx, filter, update, opts...) if err != nil { return }