Skip to content

更新单元格值时应该判断匹配的条数,而不是修改的条数 #3

@yjq635

Description

@yjq635

func (d *dao) UpdateGridValue(ctx context.Context, gridKey string, req *model.UpdateV) (err error) { c := d.mongo.Database(dbname).Collection(gridKey) filter := bson.M{"index": req.I, "celldata": bson.M{"$elemMatch": bson.M{"r": req.R, "c": req.C}}} formatV, err := d.format2Bson(req.V) if err != nil { log.With("err", err).With("v", req.V).Errorln("format2bson error") return } res, err := c.UpdateOne(ctx, filter, bson.D{{"$set", bson.D{{"celldata.$.v", formatV}}}}) if err != nil { log.With("err", err).With("gridKey", gridKey).With("req", req).Errorln("update error") return } if res.ModifiedCount <= 0 { formatCell, err := d.format2Bson(req.Cell) if err != nil { log.With("err", err).With("cell", req.Cell).Errorln("format2bson error") return err } _, err = c.UpdateOne(ctx, bson.M{"index": req.I}, bson.M{"$push": bson.M{"celldata": formatCell}}) } return err }
mongo.go:第171行这个地方 应该以匹配行数是否大于0来判断该sheet是否有这个单元格
现有的逻辑在批量设置格式时(rv操作)部分v值完全没有变化时,res.ModifiedCount就是0了,但matchedCount是1,
这个时候按现有代码逻辑 就会push一条新的数据到celldata中,最终导致celldata中同一个单元格有两个值

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions