type Student struct { ID primitive.ObjectID `bson:"_id,omitempty"` ... Users []primitive.ObjectID `json:"users"` ...}我有這個結構,我想添加到Users array. 我正在做這個并且我正在使用Mongo-go-driver// Filter for searchfilter := bson.M{"_id": userID}// Fields to updateupdate := bson.M{"$addToSet": bson.M{"users": ID}}// Update the documentUsersCollection := GetUsersCollection()UsersCollection.FindOneAndUpdate(context.Background(), filter, update, nil)有人可以告訴我我做錯了什么嗎?它不會添加到數據庫中,而是保持為空。
1 回答

函數式編程
TA貢獻1807條經驗 獲得超9個贊
更改建議:
將結構元數據標記更改json為bson
type Student struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
...
Users []primitive.ObjectID `bson:"users"`
...
}
- 1 回答
- 0 關注
- 120 瀏覽
添加回答
舉報
0/150
提交
取消