Commit 8db23cb62ad243d9864417d783b522c453dc4b43
1 parent
7cf63b79
fix: 批量删除装备 mongodb deletemany的问题
Showing
4 changed files
with
19 additions
and
6 deletions
Show diff stats
cmd/gameserver/action/RoleAction.go
| ... | ... | @@ -173,10 +173,7 @@ func EquipmentDelRpc(role *models.RoleModel, msg components.IMessage) (int32, in |
| 173 | 173 | logger.Error("loginRpc err: %v", err) |
| 174 | 174 | return 1, nil |
| 175 | 175 | } |
| 176 | - filter := make(bson.D, 5) | |
| 177 | - for _, id := range req.Id { | |
| 178 | - filter = append(filter, bson.E{Key: "id", Value: id}) | |
| 179 | - } | |
| 176 | + filter := bson.D{{"id", bson.D{{"$in", req.Id}}}} | |
| 180 | 177 | if err := mongoproxy.DelMany("equipment", filter); err != nil { |
| 181 | 178 | logger.Error(err.Error()) |
| 182 | 179 | return 2, nil | ... | ... |
cmd/gameserver/action/RoleAction_test.go
| ... | ... | @@ -2,8 +2,10 @@ package action |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | 4 | "fmt" |
| 5 | + "go.mongodb.org/mongo-driver/bson" | |
| 5 | 6 | "math/rand" |
| 6 | 7 | "pro2d/common" |
| 8 | + "pro2d/common/db/mongoproxy" | |
| 7 | 9 | "pro2d/common/db/redisproxy" |
| 8 | 10 | "pro2d/common/logger" |
| 9 | 11 | "testing" |
| ... | ... | @@ -20,3 +22,16 @@ func TestGetActionMap(t *testing.T) { |
| 20 | 22 | name := getRandomName() |
| 21 | 23 | fmt.Println(name) |
| 22 | 24 | } |
| 25 | + | |
| 26 | +func TestEquipmentDelRpc(t *testing.T) { | |
| 27 | + err := mongoproxy.ConnectMongo(common.GlobalConf.GameConf.MongoConf, common.GlobalConf.GameConf.ID) | |
| 28 | + if err != nil { | |
| 29 | + logger.Error(err) | |
| 30 | + return | |
| 31 | + } | |
| 32 | + | |
| 33 | + filter := bson.D{{"id", bson.D{{"$in", bson.A{"10000020021", "10000020037"}}}}} | |
| 34 | + if err := mongoproxy.DelMany("equipment", filter); err != nil { | |
| 35 | + logger.Error(err.Error()) | |
| 36 | + } | |
| 37 | +} | ... | ... |
common/db/mongoproxy/mongoplugin.go
| ... | ... | @@ -90,7 +90,8 @@ func DelOne(coll string, key string, value interface{}) error { |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | func DelMany(coll string, filter bson.D) error { |
| 93 | - _, err := mongoDatabase.Collection(coll).DeleteMany(context.TODO(), filter, nil) | |
| 93 | + r, err := mongoDatabase.Collection(coll).DeleteMany(context.TODO(), filter, nil) | |
| 94 | + logger.Debug(r.DeletedCount) | |
| 94 | 95 | return err |
| 95 | 96 | } |
| 96 | 97 | ... | ... |