{"id":208236,"date":"2025-07-08T17:05:09","date_gmt":"2025-07-08T09:05:09","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208236\/"},"modified":"2025-07-08T17:05:09","modified_gmt":"2025-07-08T09:05:09","slug":"golang-mongo-db-%e4%ba%8b%e5%8a%a1%e6%97%a0%e6%b3%95%e5%88%9b%e5%bb%ba%e5%91%bd%e5%90%8d%e7%a9%ba%e9%97%b4","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208236\/","title":{"rendered":"golang mongo-db \u4e8b\u52a1\u65e0\u6cd5\u521b\u5efa\u547d\u540d\u7a7a\u95f4"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>golang mongo-db \u4e8b\u52a1\u65e0\u6cd5\u521b\u5efa\u547d\u540d\u7a7a\u95f4<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-30 16:27:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300agolang mongo-db \u4e8b\u52a1\u65e0\u6cd5\u521b\u5efa\u547d\u540d\u7a7a\u95f4\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u5728 golang \u4e2d\u4f7f\u7528 mongo-db \u8fdb\u884c\u6570\u636e\u5e93\u4e8b\u52a1\uff0c\u4f46\u51fa\u73b0\u65e0\u6cd5\u521b\u5efa\u540d\u79f0\u7a7a\u95f4\u9519\u8bef<\/p>\n<pre>\/\/ for a replica set, include the replica set name and a seedlist of the members in the uri string; e.g.\n    \/\/ uri := \"mongodb:\/\/mongodb0.example.com:27017,mongodb1.example.com:27017\/?replicaset=myrepl\"\n    \/\/ for a sharded cluster, connect to the mongos instances; e.g.\n    \/\/ uri := \"mongodb:\/\/mongos0.example.com:27017,mongos1.example.com:27017\/\"\n    uri := \"mongodb:\/\/mongo-0\/block-recorder?replicaset=rs0\"\n    \/\/ var uri string\n\n    clientopts := options.client().applyuri(uri)\n    client, err := mongo.connect(ctx, clientopts)\n    if err != nil {\n        panic(err)\n    }\n    defer func() { _ = client.disconnect(ctx) }()\n\n    \/\/ prereq: create collections.\n    wcmajority := writeconcern.new(writeconcern.wmajority(), writeconcern.wtimeout(1*time.second))\n    wcmajoritycollectionopts := options.collection().setwriteconcern(wcmajority)\n    blockcollection := client.database(\"block-recorder\").collection(\"block\", wcmajoritycollectionopts)\n\n    \/\/ step 1: define the callback that specifies the sequence of operations to perform inside the transaction.\n    callback := func(sessctx mongo.sessioncontext) (interface{}, error) {\n        \/\/ important: you must pass sessctx as the context parameter to the operations for them to be executed in the\n        \/\/ transaction.\n        dbblock := model.transferblockdata(block)\n        if _, err := blockcollection.insertone(sessctx, dbblock); err != nil {\n            return nil, err\n        }\n        return nil, nil\n    }\n\n    \/\/ step 2: start a session and run the callback using withtransaction.\n    session, err := client.startsession()\n    if err != nil {\n        panic(err)\n    }\n    defer session.endsession(ctx)\n\n    result, err := session.withtransaction(ctx, callback)\n    if err != nil {\n        panic(err)\n    }\n    fmt.printf(\"result: %v\\n\", result)<\/pre>\n<p>\u8fd9\u662f\u6211\u6b63\u5728\u4f7f\u7528\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u51fa\u73b0\u4ee5\u4e0b\u9519\u8bef<\/p>\n<pre>panic: multiple write errors: [{write errors: [{Cannot create namespace block-recorder.Block in multi-document transaction.}]}, {&lt;nil&gt;}]<\/pre>\n<p>\u6211\u5927\u591a\u6570\u65f6\u5019\u90fd\u4f7f\u7528 gorm\uff0c\u8fd9\u662f\u6211\u7b2c\u4e00\u6b21\u5728 golang \u4e2d\u4f7f\u7528 mongo\uff0c\u5b83\u8bf4\u9996\u5148\u521b\u5efa\u96c6\u5408\uff0c\u6211\u5df2\u7ecf\u5728\u8fd9\u6837\u505a\u4e86\uff1f\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5728 golang \u4e2d\u4e3a mongo \u521b\u5efa\u96c6\u5408\u5417\uff1f \u5982\u4f55\u4f7f\u7528\u7ed3\u6784\u6570\u636e\u793a\u4f8b\u8fdb\u884c\u6b63\u786e\u7684\u4ea4\u6613\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>MongoDB \u670d\u52a1\u5668\u5f53\u524d\u65e0\u6cd5\u5728\u4e8b\u52a1\u4e2d\u521b\u5efa\u96c6\u5408\u3002<\/p>\n<p>\u5982\u679c\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u5c06\u6570\u636e\u63d2\u5165\u5230\u4e0d\u5b58\u5728\u7684\u96c6\u5408\u4e2d\uff0c\u5219\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u670d\u52a1\u5668\u4f1a\u900f\u660e\u5730\u521b\u5efa\u8be5\u96c6\u5408\u3002\u4f46\u5982\u679c\u4ea4\u6613\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\uff0c\u5219\u76ee\u524d\u6b64\u529f\u80fd\u4e0d\u8d77\u4f5c\u7528\u3002<\/p>\n<p>\u63d0\u524d\u521b\u5efa\u96c6\u5408\uff0c\u4ee5\u4fbf\u5b83\u5728\u4e8b\u52a1\u6267\u884c\u65f6\u5b58\u5728\u3002<\/p>\n<p>\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u4f8b\u5316\u96c6\u5408\u5bf9\u8c61\u5b9e\u9645\u4e0a\u5e76\u4e0d\u521b\u5efa\u96c6\u5408\u3002\u8981\u521b\u5efa\u96c6\u5408\uff0c\u8bf7\u5c1d\u8bd5 go \u9a71\u52a8\u7a0b\u5e8f\u4e2d\u76f8\u5f53\u4e8e \u7684\u5185\u5bb9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208236","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208236"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208236\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}