From 8db23cb62ad243d9864417d783b522c453dc4b43 Mon Sep 17 00:00:00 2001 From: zqj <582132116@qq.com> Date: Fri, 20 May 2022 18:16:32 +0800 Subject: [PATCH] fix: 批量删除装备 mongodb deletemany的问题 --- DesignerConfigs | 2 +- cmd/gameserver/action/RoleAction.go | 5 +---- cmd/gameserver/action/RoleAction_test.go | 15 +++++++++++++++ common/db/mongoproxy/mongoplugin.go | 3 ++- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/DesignerConfigs b/DesignerConfigs index 4011876..ca2a212 160000 --- a/DesignerConfigs +++ b/DesignerConfigs @@ -1 +1 @@ -Subproject commit 4011876adebf1d046b150ec77e1714d25aa7cb35 +Subproject commit ca2a212e0e5ed06b6f33e3c4bd4570976a169010 diff --git a/cmd/gameserver/action/RoleAction.go b/cmd/gameserver/action/RoleAction.go index d541a18..4627db5 100644 --- a/cmd/gameserver/action/RoleAction.go +++ b/cmd/gameserver/action/RoleAction.go @@ -173,10 +173,7 @@ func EquipmentDelRpc(role *models.RoleModel, msg components.IMessage) (int32, in logger.Error("loginRpc err: %v", err) return 1, nil } - filter := make(bson.D, 5) - for _, id := range req.Id { - filter = append(filter, bson.E{Key: "id", Value: id}) - } + filter := bson.D{{"id", bson.D{{"$in", req.Id}}}} if err := mongoproxy.DelMany("equipment", filter); err != nil { logger.Error(err.Error()) return 2, nil diff --git a/cmd/gameserver/action/RoleAction_test.go b/cmd/gameserver/action/RoleAction_test.go index c460b29..e961432 100644 --- a/cmd/gameserver/action/RoleAction_test.go +++ b/cmd/gameserver/action/RoleAction_test.go @@ -2,8 +2,10 @@ package action import ( "fmt" + "go.mongodb.org/mongo-driver/bson" "math/rand" "pro2d/common" + "pro2d/common/db/mongoproxy" "pro2d/common/db/redisproxy" "pro2d/common/logger" "testing" @@ -20,3 +22,16 @@ func TestGetActionMap(t *testing.T) { name := getRandomName() fmt.Println(name) } + +func TestEquipmentDelRpc(t *testing.T) { + err := mongoproxy.ConnectMongo(common.GlobalConf.GameConf.MongoConf, common.GlobalConf.GameConf.ID) + if err != nil { + logger.Error(err) + return + } + + filter := bson.D{{"id", bson.D{{"$in", bson.A{"10000020021", "10000020037"}}}}} + if err := mongoproxy.DelMany("equipment", filter); err != nil { + logger.Error(err.Error()) + } +} diff --git a/common/db/mongoproxy/mongoplugin.go b/common/db/mongoproxy/mongoplugin.go index 55fedd3..07a1e59 100644 --- a/common/db/mongoproxy/mongoplugin.go +++ b/common/db/mongoproxy/mongoplugin.go @@ -90,7 +90,8 @@ func DelOne(coll string, key string, value interface{}) error { } func DelMany(coll string, filter bson.D) error { - _, err := mongoDatabase.Collection(coll).DeleteMany(context.TODO(), filter, nil) + r, err := mongoDatabase.Collection(coll).DeleteMany(context.TODO(), filter, nil) + logger.Debug(r.DeletedCount) return err } -- libgit2 0.21.2