openimsdk / open-im-server

IM Chat ChatGPT
https://openim.io
Apache License 2.0
14.12k stars 2.49k forks source link

[BUG] mongodb transaction failed: mongo find one and update: Failed to parse update: field must be of BSON type object: Failed to parse update: field must be of BSON type object"} #2892

Open hejianbing opened 1 day ago

hejianbing commented 1 day ago

OpenIM Server Version

v3.8.2

Operating System and CPU Architecture

Linux (AMD)

Deployment Method

Source Code Deployment

Bug Description and Steps to Reproduce

func (l *VersionLogMgo) writeLogBatch2(ctx context.Context, dId string, eIds []string, state int32, now time.Time) (*model.VersionLog, error) { if eIds == nil { eIds = []string{} } filter := bson.M{ "d_id": dId, } elems := make([]bson.M, 0, len(eIds)) for _, eId := range eIds { elems = append(elems, bson.M{ "e_id": eId, "version": "$version", "state": state, "last_update": now, }) } pipeline := []bson.M{ { "$addFields": bson.M{ "delete_e_ids": eIds, }, }, { "$set": bson.M{ "version": bson.M{"$add": []any{"$version", 1}}, "last_update": now, }, }, { "$set": bson.M{ "logs": bson.M{ "$filter": bson.M{ "input": "$logs", "as": "log", "cond": bson.M{ "$not": bson.M{ "$in": []any{"$$log.e_id", "$delete_e_ids"}, }, }, }, }, }, }, { "$set": bson.M{ "logs": bson.M{ "$concatArrays": []any{ "$logs", elems, }, }, }, }, { "$unset": "delete_e_ids", }, } projection := bson.M{ "logs": 0, } opt := options.FindOneAndUpdate().SetUpsert(false).SetReturnDocument(options.After).SetProjection(projection) res, err := mongoutil.FindOneAndUpdate[*model.VersionLog](ctx, l.coll, filter, pipeline, opt) if err != nil { return nil, err } res.Logs = make([]model.VersionLogElem, 0, len(eIds)) for _, id := range eIds { res.Logs = append(res.Logs, model.VersionLogElem{ EID: id, State: state, Version: res.Version, LastUpdate: res.LastUpdate, }) } return res, nil } 拼接BSON 格式错误

Screenshots Link

No response

skiffer-git commented 1 day ago

Under what circumstances does this issue occur?