需要協助從 mongodb 文件獲取元素並使用 go mongo 驅動程式更新其值
php小編草莓在這裡為大家介紹一個常見的需求:如何從MongoDB文件中獲取元素並使用Go Mongo驅動程式更新其值。 MongoDB是一種流行的NoSQL資料庫,而Go是一種強大的程式語言,兩者的結合可以帶來許多好處。在本文中,我們將探討如何使用Go Mongo驅動程式來實現這個需求,幫助您更能理解並應用這個技術。
問題內容
嘿,我正在使用 go.mongodb.org/mongo-driver 套件來使用 golang。這是我的程式碼:
package src import ( "context" "fmt" "log" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) var ( client *mongo.client stickerscol *mongo.collection ) func init() { log.println("setting up database...") var err error client, err = mongo.connect(context.background(), options.client().applyuri(envars.dburl)) if err != nil { log.fatal(err) } log.println("connected to database") stickerscol = client.database("stickersbot").collection("stickers") chatcol = client.database("stickersbot").collection("chat") } func changedefaultpack(userid int64, stickername string, format string) error { fmt.println(userid, stickername, format) filter := bson.m{ "_id": userid, "stickers.stickername": stickername, "stickers.stickerpacktype": format, } updateother := bson.m{ "$set": bson.m{ "stickers.$[elem].isdefault": false, }, } update := bson.m{ "$set": bson.m{ "stickers.$.isdefault": true, }, } arrayfilters := options.arrayfilters{ filters: []interface{}{bson.m{"elem.stickerpacktype": format}}, } updateoptions := options.update().setarrayfilters(arrayfilters) _, err := stickerscol.updatemany(context.todo(), filter, updateother, updateoptions) if err != nil { return err } ok, err := stickerscol.updateone(context.background(), filter, update, options.update().setupsert(true)) if err != nil { return err } if ok.modifiedcount == 0 { fmt.println("no document matched") return fmt.errorf("no document matched") } return err }
這是資料庫結構:
... { "_id": 1633375527, "stickers": [ { "stickerpacktype": "static", "stickername": "gVtOy_1633375527_by_ChadManBot", "isdefault": true }, { "stickerpacktype": "animated", "stickername": "zIjsa_1633375527_by_ChadManBot", "isdefault": true }, { "stickerpacktype": "video", "stickername": "bJBle_1633375527_by_ChadManBot", "isdefault": true }, { "stickerpacktype": "static", "stickername": "iujiw_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "video", "stickername": "CHnqb_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "XKJUP_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "animated", "stickername": "pCFlC_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "animated", "stickername": "jGGgt_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "animated", "stickername": "cTxyA_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "animated", "stickername": "ZfYXO_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "xJUkA_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "HIvsY_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "WIHFQ_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "OqjtE_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "QNysO_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "OrTsT_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "FzROg_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "QUBcT_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "NsZfM_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "AkxVM_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "Ddvus_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "LXfHV_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "sTaMv_1633375527_by_ChadManBot", "isdefault": false }, { "stickerpacktype": "static", "stickername": "vppgK_1633375527_by_ChadManBot", "isdefault": false } ], "stickerpackcount": 2 } ...
我需要我的程式碼是找到與給定id 和貼圖類型相符的貼紙包列表,並將其預設值設為false
並將傳遞給func 的包的預設值設為true
。
基本上只是將其中一個設定為預設值,而其他設定為非預設值。有人可以解釋為什麼這個函數回傳未找到文件嗎?
解決方法
當有多個欄位符合時,您應該使用 $elemmatch
運算子。否則,$
運算子不起作用。請參閱 使用多個欄位符合更新嵌入文件。
且您不需要 updatemany
來更新單一文件中的多個陣列元素。請改用 updateone
。
這是基於您的程式碼和資料的示範:
package main import ( "context" "fmt" "log" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) var ( client *mongo.client stickerscol *mongo.collection ) func init() { log.println("setting up database...") var err error client, err = mongo.connect(context.background(), options.client().applyuri("mongodb://localhost")) if err != nil { log.fatal(err) } log.println("connected to database") stickerscol = client.database("stickersbot").collection("stickers") } func changedefaultpack(userid int64, stickername string, format string) error { fmt.println(userid, stickername, format) filter := bson.m{ "_id": userid, "stickers": bson.m{ "$elemmatch": bson.m{ "stickername": stickername, "stickerpacktype": format, }, }, } updateother := bson.m{ "$set": bson.m{ "stickers.$[elem].isdefault": false, }, } arrayfilters := options.arrayfilters{ filters: []interface{}{bson.m{"elem.stickerpacktype": format}}, } updateoptions := options.update().setarrayfilters(arrayfilters) result, err := stickerscol.updateone(context.background(), filter, updateother, updateoptions) if err != nil { return err } log.printf("update other: %+v\n", result) update := bson.m{ "$set": bson.m{ "stickers.$.isdefault": true, }, } ok, err := stickerscol.updateone(context.background(), filter, update, options.update().setupsert(true)) if err != nil { return err } log.printf("update default: %+v\n", ok) if ok.modifiedcount == 0 { fmt.println("no document matched") return fmt.errorf("no document matched") } return err } func main() { if err := changedefaultpack(1633375527, "ddvus_1633375527_by_chadmanbot", "static"); err != nil { panic(err) } }
執行演示,您將觀察到文件發生以下變更:
--- before.json 2023-06-27 00:34:32.721978745 +0800 after.json 2023-06-27 00:34:59.489836137 +0800 @@ -4,7 +4,7 @@ { "stickerpacktype": "static", "stickername": "gVtOy_1633375527_by_ChadManBot", - "isdefault": true + "isdefault": false }, { "stickerpacktype": "animated", @@ -104,7 +104,7 @@ { "stickerpacktype": "static", "stickername": "Ddvus_1633375527_by_ChadManBot", - "isdefault": false + "isdefault": true }, { "stickerpacktype": "static",
以上是需要協助從 mongodb 文件獲取元素並使用 go mongo 驅動程式更新其值的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Golang在性能和可擴展性方面優於Python。 1)Golang的編譯型特性和高效並發模型使其在高並發場景下表現出色。 2)Python作為解釋型語言,執行速度較慢,但通過工具如Cython可優化性能。

Golang在並發性上優於C ,而C 在原始速度上優於Golang。 1)Golang通過goroutine和channel實現高效並發,適合處理大量並發任務。 2)C 通過編譯器優化和標準庫,提供接近硬件的高性能,適合需要極致優化的應用。

goisidealforbeginnersandsubableforforcloudnetworkservicesduetoitssimplicity,效率和concurrencyFeatures.1)installgromtheofficialwebsitealwebsiteandverifywith'.2)

Golang適合快速開發和並發場景,C 適用於需要極致性能和低級控制的場景。 1)Golang通過垃圾回收和並發機制提升性能,適合高並發Web服務開發。 2)C 通過手動內存管理和編譯器優化達到極致性能,適用於嵌入式系統開發。

Golang和Python各有优势:Golang适合高性能和并发编程,Python适用于数据科学和Web开发。Golang以其并发模型和高效性能著称,Python则以简洁语法和丰富库生态系统著称。

Golang和C 在性能上的差異主要體現在內存管理、編譯優化和運行時效率等方面。 1)Golang的垃圾回收機制方便但可能影響性能,2)C 的手動內存管理和編譯器優化在遞歸計算中表現更為高效。

Golang和C 在性能競賽中的表現各有優勢:1)Golang適合高並發和快速開發,2)C 提供更高性能和細粒度控制。選擇應基於項目需求和團隊技術棧。

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t
