我正在嘗試使用連接到 Azure CosmosDB 實(shí)例 (v3.6)的 mongodb/mongo-go-driver (v1.2.1) 編寫一個(gè)簡(jiǎn)單的 CRUD 測(cè)試應(yīng)用程序??紤]以下代碼摘錄。Update從結(jié)構(gòu)中剝離函數(shù),Client為簡(jiǎn)潔起見(jiàn)省略func (c *Client) Update(ctx context.Context, filter, update bson.M) error { res, err := c.collection.UpdateOne(ctx, filter, update, options.Update()) if err != nil { return Error{ErrFunctional, "failed to update document", err} } if res.MatchedCount != 1 { return Error{ Code: ErrNotFound, msg: "document not found", } } if res.ModifiedCount != 1 { return Error{ Code: ErrNotUpdated, msg: "document not updated", } } return nil}Runner 代碼如下所示type doc struct { count int}id, err := dbClient.Insert(context.TODO(), doc{1})if err != nil { panic(fmt.Errorf("insert failed: %v", err))}err = dbClient.Update(context.TODO(), bson.M{"_id": id}, bson.M{"$set": bson.M{"count": 2}})if err != nil { panic(fmt.Errorf("update failed: %v", err))}err = dbClient.Delete(context.TODO(), bson.M{"_id": id})if err != nil { panic(fmt.Errorf("delete failed: %v", err))}正如您在代碼中看到的,我正在嘗試完成以下步驟:插入記錄{"count": 1}(這可以正常工作并插入文檔)將插入記錄更新為{"count": 2}(Fails due to no document found 錯(cuò)誤)刪除記錄(代碼永遠(yuǎn)不會(huì)到達(dá)這里)程序在第二步失敗。我檢查了驅(qū)動(dòng)程序返回的結(jié)果,兩者M(jìn)atchedCount都ModifiedCount為0。但是數(shù)據(jù)庫(kù)已更新為正確的數(shù)據(jù)。很奇怪,對(duì)吧?現(xiàn)在有趣的是,如果我使用 MongoDB shell(CLI,使用 brew 安裝)執(zhí)行相同的步驟,那么這些步驟將毫無(wú)問(wèn)題地完成。我已經(jīng)嘗試了過(guò)濾器和更新語(yǔ)句的所有變體以使其工作,但無(wú)濟(jì)于事。我有一種感覺(jué),它與 Golang 驅(qū)動(dòng)程序有關(guān)。我有什么遺漏或做錯(cuò)了嗎?請(qǐng)隨時(shí)詢問(wèn)更多信息,我很樂(lè)意編輯問(wèn)題以提供它。
1 回答
倚天杖
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
原來(lái)這只是連接字符串中的一個(gè)愚蠢的錯(cuò)誤。出于某種原因,我使用了這種格式。
mongodb://<username>:<password>@<app>.documents.azure.com:10255/?ssl=true&replicaSet=globaldb&maxIdleTimeMS=120000&retrywrites=false"
更改為這種格式,現(xiàn)在一切正常。
mongodb://<username>:<password>@<app>.mongo.cosmos.azure:10255/?ssl=true&replicaSet=globaldb&maxIdleTimeMS=120000&retrywrites=false"
SMH。
- 1 回答
- 0 關(guān)注
- 141 瀏覽
添加回答
舉報(bào)
0/150
提交
取消
