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

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

如何刪除 Mongodb / Golang 中的數組項?

如何刪除 Mongodb / Golang 中的數組項?

Go
慕村9548890 2021-12-07 18:57:48
我有以下數據結構,我試圖從“藝術家”數組中刪除一個項目。[    {        "id": "56b26eeb4a876400011369e9",        "name": "Ewan Valentine",        "email": "[email protected]",        "artists": [            "56b26f334a876400011369ea",            "56b2702881318d0001dd1441",            "56b2746fdf1d7e0001faaa92",        ],        "user_location": "Manchester, UK"    }]這是我的功能...// Remove artist from userfunc (repo *UserRepo) RemoveArtist(userId string, artistId string) error {    change := bson.M{"artists": bson.M{"$pull": bson.ObjectIdHex(artistId)}}    fmt.Println(userId)    err := repo.collection.UpdateId(bson.ObjectIdHex(userId), change)    return err}我收到以下錯誤:{  "_message": {    "Err": "The dollar ($) prefixed field '$pull' in 'artists.$pull' is not valid for storage.",    "Code": 52,    "N": 0,    "Waited": 0,    "FSyncFiles": 0,    "WTimeout": false,    "UpdatedExisting": false,    "UpsertedId": null  }}
查看完整描述

1 回答

?
慕田峪4524236

TA貢獻1875條經驗 獲得超5個贊

$pull運營商在更新語句的“頂級”操作,所以只需有這周圍的錯誤的方式:

    change := bson.M{"$pull": bson.M{"artists": bson.ObjectIdHex(artistId)}}

更新運算符的順序始終是運算符第一,動作第二。

如果在“頂級”鍵上沒有運算符,MongoDB 會將其解釋為只是一個“普通對象”來更新和“替換”匹配的文檔。因此,關于$密鑰名稱中的錯誤。


查看完整回答
反對 回復 2021-12-07
  • 1 回答
  • 0 關注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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