這是我在 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"
- 1 回答
- 0 關注
- 130 瀏覽
添加回答
舉報
0/150
提交
取消