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

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

在文檔 mongodb mgo 驅動程序中增加嵌套數組的特定值

在文檔 mongodb mgo 驅動程序中增加嵌套數組的特定值

Go
大話西游666 2023-05-22 17:19:48
這是我在 json 中的 mongo 集合:"messages" : {"_id" : ObjectId("5c26844696b72e4b6c9ceee7"),"pointer" : ObjectId("5c26844696b72e4b6c9ceee6"),"messages" : [    {       "uuid" : "f03f7977-0b4e-11e9-9f95-144fd7c03810",       "content" : "Hello",       "reportedTimes":0   },    {       "uuid" : "78bb831d-0b57-11e9-a286-144fd7c03810",       "content" : "Yes",       "reportedTimes":0   }  ]}我想從“指針”和“uuid”更新“reportedTimes”值。我嘗試了很多聚合,但沒有像那個那樣的結果:pipe := []bson.M{bson.M{"$match": bson.M{"pointer": knownPointer}}, {"messages": bson.M{"$match": bson.M{"uuid": knownUUID}}}, {"$inc": bson.M{"messages.$.reportedTimes": 1}}}那個返回我不能使用“$inc”或者那個pipe := []bson.M{{"$match": bson.M{"pointer": knownPointer}}, {"$unwind": "$messages"}, {"$project": bson.M{"uuid": "$messages.uuid", "reportedTimes": "$messages.reportedTimes"}}, {"$match": bson.M{"uuid": knownUuid}}} then inc.我什至沒有找到我所有的嘗試查詢......我完全被卡住了......我也嘗試過 Update(selector, query) 但仍然找不到工作和有效的方法來做到這一點。一點幫助將不勝感激。謝謝大家。
查看完整描述

1 回答

?
幕布斯6054654

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

這是一個簡單的Collection.Update()操作,不需要為此使用聚合:

err := c.Update(bson.M{

? ? "pointer":? ? ? ?knownPointer,

? ? "messages.uuid": knownUUID,

}, bson.M{

? ? "$inc": bson.M{

? ? ? ? "messages.$.reportedTimes": 1,

? ? },

})

以下是設置參數以增加reportedTimes文檔第一條消息的方法:


knownPointer := bson.ObjectIdHex("5c26844696b72e4b6c9ceee6")

knownUUID := "f03f7977-0b4e-11e9-9f95-144fd7c03810"


查看完整回答
反對 回復 2023-05-22
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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