{"id":10008119,"date":"2023-08-20T14:00:01","date_gmt":"2023-08-20T06:00:01","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008119"},"modified":"2023-08-20T14:00:01","modified_gmt":"2023-08-20T06:00:01","slug":"mongodb-go-driver-%e5%a6%82%e4%bd%95%e8%ae%b0%e5%bd%95%e6%97%a5%e5%bf%97","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008119","title":{"rendered":"MongoDB Go Driver \u5982\u4f55\u8bb0\u5f55\u65e5\u5fd7"},"content":{"rendered":"<h2>\u4e3a\u4ec0\u4e48<\/h2>\n<p>\u5982\u679c\u4f60\u6709\u8fd9\u4e9b\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>golang \u5b98\u65b9\u7684 mongo driver \u600e\u4e48\u6267\u884c\u4e86\u6ca1\u6709\u65e5\u5fd7\u8f93\u51fa\u554a<\/li>\n<li>\n<p>golang mongo driver \u5982\u4f55\u8bb0\u5f55sql\uff0c\u5982\u4f55\u6253\u5370\u4e4b\u884c\u7684\u547d\u4ee4<br \/>\u90a3\u4e48\u8fd9\u7bc7\u6587\u7ae0\u53ef\u4ee5\u7ee7\u7eed\u770b\u4e0b\u53bb\u3002<\/p>\n<h2>\u80cc\u666f<\/h2>\n<\/li>\n<\/ol>\n<p>\u5728\u8fd9\u4e4b\u524d\u6211\u4f7f\u7528 MySQL\u662f\u6bd4\u8f83\u591a\u7684\uff0c\u540e\u6765\u9047\u5230\u6709\u8bb0\u5f55\u7528\u6237\u65e5\u5fd7\u9700\u6c42\uff0c\u968f\u7740\u6570\u636e\u91cf\u8d8a\u6765\u8d8a\u5927\uff0cMySQL\u8d8a\u6765\u8d8a\u625b\u4e0d\u4f4f\uff0c\u5c31\u5347\u7ea7\u4e3a\u4e86Mongo\uff0c\u5728\u4f7f\u7528 Mongo\u8fc7\u7a0b\u4e2d\uff0c\u53d1\u73b0\u6ca1\u6cd5\u8bb0\u5f55\u65e5\u5fd7\uff0c\u5c31\u662f\u65e0\u6cd5\u5c06\u6253\u5370\u4ee3\u7801\u53d1\u8d77\u7684sql\uff0c\u4e4b\u524d\u4f7f\u7528gorm\u6253\u5370execute sql \u662f\u5f88\u65b9\u4fbf\u7684\u3002<\/p>\n<p>\u5728\u7f51\u4e0a\u67e5\u4e86\u597d\u4e45\u4e5f\u6ca1\u67e5\u5230\u76f8\u5173\u8d44\u6599\uff0c\u5c31\u5f00\u59cb\u7ffb\u5b98\u65b9\u6587\u6863\uff0c\u548c\u6e90\u7801\u3002<\/p>\n<p>\u7ec8\u4e8e\u5728\u4e0d\u61c8~ \u6253\u4f4f\uff0c\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u5c31\u5728 <code>ClientOptions<\/code> \u5b57\u6bb5\u91cc\uff1a<\/p>\n<pre><code class=\"go\">\/\/ ClientOptions contains options to configure a Client instance. Each option can be set through setter functions. See\n\/\/ documentation for each setter function for an explanation of the option.\ntype ClientOptions struct {\n    AppName                  *string\n    Auth                     *Credential\n    AutoEncryptionOptions    *AutoEncryptionOptions\n    ...\n    MaxConnecting            *uint64\n    PoolMonitor              *event.PoolMonitor\n    Monitor                  *event.CommandMonitor \/\/ \u6267\u884c\u7684\u547d\u4ee4\u76d1\u89c6\u5668(\u65e5\u5fd7)\n    ServerMonitor            *event.ServerMonitor\n    ...\n\n    err error\n    uri string\n    cs  *connstring.ConnString<\/code><\/pre>\n<p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=q7Z8HCSxa1t6Z3EGKoGrBQ%3D%3D.vW349Mxi6ZsPPbmbgY8pIZgs6oGwNTjl4awfNzVTei%2BlFbfX9dqsbhEhdtCPOKCwgnoODRkREL2Ln2saMBD8iukFaGs8qmWdMsxGGO61XmHxdBM%2FzKIY5uStHx38QK73rhi7zt1uVyPAjLEfSFU9PyV3ZuLKe7eUD3E2fAOLDBLLEP6HnXK0Uq6n7hgfrQ0F\" rel=\"noopener noreferrer\">github \u4ee3\u7801\u5730\u5740<\/a><\/p>\n<p>\u662f\u7684\u2014\u2014 <code>ClientOptions.Monitor<\/code> \u5b57\u6bb5\u3002<\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"go\">package mongolearn\n\nimport (\n    &quot;context&quot;\n    &quot;fmt&quot;\n    &quot;log&quot;\n    &quot;time&quot;\n\n    &quot;go.mongodb.org\/mongo-driver\/bson&quot;\n    &quot;go.mongodb.org\/mongo-driver\/event&quot;\n    &quot;go.mongodb.org\/mongo-driver\/mongo&quot;\n    &quot;go.mongodb.org\/mongo-driver\/mongo\/options&quot;\n)\n\nvar (\n    mongoDsn = &quot;mongodb:\/\/admin:123456@127.0.0.1:27017&quot;\n)\n\n\/\/ TestConnUseDb sql monitor\nfunc TestConnWithMonitor() {\n    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)\n    defer cancel()\n\n    \/\/ client option\n    var clientOpt = options.Client().ApplyURI(mongoDsn)\n    \n    \/\/ log monitor\n    var logMonitor = event.CommandMonitor{\n        Started: func(ctx context.Context, startedEvent *event.CommandStartedEvent) {\n            log.Printf(&quot;mongo reqId:%d start on db:%s cmd:%s sql:%+v&quot;, startedEvent.RequestID, startedEvent.DatabaseName,\n                startedEvent.CommandName, startedEvent.Command)\n        },\n        Succeeded: func(ctx context.Context, succeededEvent *event.CommandSucceededEvent) {\n            log.Printf(&quot;mongo reqId:%d exec cmd:%s success duration %d ns&quot;, succeededEvent.RequestID,\n                succeededEvent.CommandName, succeededEvent.DurationNanos)\n        },\n        Failed: func(ctx context.Context, failedEvent *event.CommandFailedEvent) {\n            log.Printf(&quot;mongo reqId:%d exec cmd:%s failed duration %d ns&quot;, failedEvent.RequestID,\n                failedEvent.CommandName, failedEvent.DurationNanos)\n        },\n    }\n    \/\/ cmd monitor set\n    clientOpt.SetMonitor(&amp;logMonitor)\n    client, err := mongo.Connect(ctx, clientOpt)\n    if nil != err {\n        fmt.Printf(&quot;mongo connect err %v\/n&quot;, err)\n    } else {\n        fmt.Printf(&quot;mongo connect success~\/n&quot;)\n        defer func() {\n            if err = client.Disconnect(ctx); err != nil {\n                panic(err)\n            }\n        }()\n    }\n\n    \/\/ update test\n    if re, err := client.Database(&quot;test&quot;).Collection(&quot;test&quot;).UpdateOne(ctx, bson.M{&quot;name&quot;: &quot;cc&quot;}, bson.M{&quot;$set&quot;: bson.M{&quot;age&quot;: 12}}); err != nil {\n        log.Printf(&quot;%v&quot;, err)\n    } else {\n        log.Printf(&quot;mongo update one re %+v&quot;, re)\n    }\n}<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c<\/p>\n<pre><code>mongo connect success~\n2023\/08\/20 13:22:43 mongo reqId:6 start on db:test cmd:update sql:{&quot;update&quot;: &quot;test&quot;,&quot;ordered&quot;: true,&quot;lsid&quot;: {&quot;id&quot;: {&quot;$binary&quot;:{&quot;base64&quot;:&quot;qfrrzSt7SkCN5ChY04\/T5A==&quot;,&quot;subType&quot;:&quot;04&quot;}}},&quot;$db&quot;: &quot;test&quot;,&quot;updates&quot;: [{&quot;q&quot;: {&quot;name&quot;: &quot;cc&quot;},&quot;u&quot;: {&quot;$set&quot;: {&quot;age&quot;: {&quot;$numberInt&quot;:&quot;12&quot;}}}}]}\n2023\/08\/20 13:22:43 mongo reqId:6 exec cmd:update success duration 44489114 ns\n2023\/08\/20 13:22:43 mongo update one re &amp;{MatchedCount:0 ModifiedCount:0 UpsertedCount:0 UpsertedID:&lt;nil&gt;}\n\n# \u4e0b\u9762\u662f\u65ad\u5f00 mongo \u65f6\u89e6\u53d1\u7684\u547d\u4ee4\n2023\/08\/20 13:22:43 mongo reqId:7 start on db:admin cmd:endSessions sql:{&quot;endSessions&quot;: [{&quot;id&quot;: {&quot;$binary&quot;:{&quot;base64&quot;:&quot;qfrrzSt7SkCN5ChY04\/T5A==&quot;,&quot;subType&quot;:&quot;04&quot;}}}],&quot;$db&quot;: &quot;admin&quot;}\n2023\/08\/20 13:22:43 mongo reqId:7 exec cmd:endSessions success duration 58037162 ns<\/code><\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65e5\u5fd7\u53ef\u4ee5\u770b\u5230\uff0cmongo \u7684monitor \u6309\u7167 Started, Succeeded \u987a\u5e8f\u8bb0\u5f55\uff0c\u6700\u540e\u624d\u4f1a\u6267\u884c\u51fd\u6570\u5916\u7684 \u65e5\u5fd7 \u3002<\/p>\n<h2>\u7ed3\u675f<\/h2>\n<p>\u6211\u770b\u7f51\u4e0a\u8d44\u6599\u5c11\uff0c\u5f88\u591a\u5c0f\u4f19\u4f34\u90fd\u4e0d\u77e5\u9053\uff0c\u7279\u6b64\u5199\u4e00\u7bc7\u5c0f\u6587\u8bb0\u5f55\u4e0b\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044131316\">https:\/\/segmentfault.com\/a\/1190000044131316<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48 \u5982\u679c\u4f60\u6709\u8fd9\u4e9b\u95ee\u9898\uff1a golang \u5b98\u65b9\u7684 mongo driver \u600e\u4e48\u6267\u884c\u4e86\u6ca1\u6709\u65e5\u5fd7\u8f93\u51fa\u554a gola [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008119","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008119"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008119\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}