亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 struct & mongodb/mongo-go-driver 更新/替換 mongodb

使用 struct & mongodb/mongo-go-driver 更新/替換 mongodb

Go
烙印99 2023-04-17 16:45:04
我正在嘗試使用結構更新/替換 mongodb 文檔,但我一直在獲取err: update document must contain key beginning with '$'collection := r.client.Database(database).Collection(greetingCollection)payment.MongoID = objectid.New()filter := bson.NewDocument(bson.EC.String("id", payment.ID))_, err := collection.UpdateOne(ctx, filter, payment)return err
查看完整描述

2 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

我相信接受的答案對我不起作用,因為我正在使用該go.mongodb.org/mongo-driver軟件包。有了這個包,語法就更簡單了:

update := bson.M{ 
       "$set": yourDocument,
    }

collection.UpdateOne(ctx, filter, update)


查看完整回答
反對 回復 2023-04-17
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

您應該提供更新語句而不是文檔作為該Collection.UpdateOne方法的第三個參數。例如:


update := bson.NewDocument(

? ? bson.EC.SubDocumentFromElements(

? ? ? ? "$set",

? ? ? ? bson.EC.Double("pi", 3.14159),

? ? ),

)

collection.UpdateOne(ctx, filter, update)

在MongoDB 文檔中查看有關可用更新運算符的更多信息(鍵以“$”開頭)。



查看完整回答
反對 回復 2023-04-17
  • 2 回答
  • 0 關注
  • 360 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號