{"id":207932,"date":"2025-07-08T08:44:25","date_gmt":"2025-07-08T00:44:25","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207932\/"},"modified":"2025-07-08T08:44:25","modified_gmt":"2025-07-08T00:44:25","slug":"%e5%a6%82%e4%bd%95%e5%b0%86-mongodb-%e6%96%87%e6%a1%a3%e4%b8%ad%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%a7%a3%e7%a0%81%e4%b8%ba-go-%e4%b8%ad%e7%9a%84%e8%87%aa%e5%ae%9a%e4%b9%89%e4%b8%9a%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207932\/","title":{"rendered":"\u5982\u4f55\u5c06 MongoDB \u6587\u6863\u4e2d\u7684\u5b57\u7b26\u4e32\u89e3\u7801\u4e3a Go \u4e2d\u7684\u81ea\u5b9a\u4e49\u4e1a\u52a1\u7ed3\u6784\uff1f"},"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>\u5982\u4f55\u5c06 MongoDB \u6587\u6863\u4e2d\u7684\u5b57\u7b26\u4e32\u89e3\u7801\u4e3a Go \u4e2d\u7684\u81ea\u5b9a\u4e49\u4e1a\u52a1\u7ed3\u6784\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 22:30: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>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5c06 MongoDB \u6587\u6863\u4e2d\u7684\u5b57\u7b26\u4e32\u89e3\u7801\u4e3a Go \u4e2d\u7684\u81ea\u5b9a\u4e49\u4e1a\u52a1\u7ed3\u6784\uff1f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u4e1a\u52a1\u5bf9\u8c61\uff0c\u5b83\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u201c\u4ee3\u7801\u201d\uff0c\u9700\u8981\u5c06\u5176\u4fdd\u5b58\u5728 mongodb \u7684\u6587\u6863\u4e2d\u3002\u5f53\u4ece mongodb \u83b7\u53d6\u6587\u6863\u65f6\uff0c\u6211\u9700\u8981\u5c06\u4ee3\u7801\u8f6c\u6362\u4e3a\u6211\u4eec\u7684 clientcode \u4e1a\u52a1\u5bf9\u8c61\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u66f4\u8be6\u7ec6\u5730\u8bf4\uff1a<\/p>\n<pre>business objects - simplified\n\ntype clientcode struct {\n  code string `bson:\"code\" json:\"code\"`\n}\n\ntype project struct {\n  name string `bson:\"name\" json:\"name\"`\n  code clientcode `bson:\"clientcode\" json:\"clientcode\"`\n}\n\np := project{\n  name: \"abc\",\n  code: clientcode{code: \"abccorp\"} \n}<\/pre>\n<p>\u6211\u60f3\u6ce8\u518c\u4e00\u4e2a\u8f6c\u6362\u5668\uff0c\u5c06\u9879\u76ee\u7684\u6b64\u5b9e\u4f8b\u5e8f\u5217\u5316\u4e3a\u6570\u636e\u5e93\u96c6\u5408\uff1aprojects\uff0c<\/p>\n<pre>[\n  {\n    \"name\":\"Abc\",\n    \"code\":\"abccorp\"\n  }\n]<\/pre>\n<p>\u6211\u60f3\u6ce8\u518c\u4e00\u4e2a\u8f6c\u6362\u5668\uff0c\u5c06\u6570\u636e\u5e93\u4e2d\u7684\u9879\u76ee\u6587\u6863\u53cd\u5e8f\u5217\u5316\u4e3a project \u5b9e\u4f8b\u3002\u6b64\u8fc7\u7a0b\u8fd8\u5fc5\u987b\u53cd\u5e8f\u5217\u5316 clientcode \u5b57\u6bb5\u3002<\/p>\n<p>\u6211\u5728 mongodb \u6587\u6863\u4e2d\u627e\u4e0d\u5230\u592a\u591a\u6709\u5173\u4e3a\u5d4c\u5165\u5f0f go \u7ed3\u6784\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7f16\u7801\u5668\/\u89e3\u7801\u5668\u7684\u4fe1\u606f\u3002\u6211\u5df2\u7ecf\u5728\u57fa\u4e8e kotlin \u548c spring boot \u7684\u7c7b\u4f3c webapi \u670d\u52a1\u4e2d\u5b9e\u73b0\u4e86\u81ea\u5b9a\u4e49\u8f6c\u6362\u5668\u3002\u5b83\u4f7f\u7528\u6ce8\u518c\u8f6c\u6362\u5668\u5e76\u81ea\u52a8\u6267\u884c\u6bcf\u4e2a\u65b9\u5411\u7684\u8f6c\u6362\u3002\u6211\u975e\u5e38\u611f\u8c22\u4efb\u4f55\u6709\u5173\u5982\u4f55\u5728 go \u4e2d\u5b8c\u6210\u6b64\u4efb\u52a1\u7684\u63a8\u52a8\u6216\u5efa\u8bae\u3002<\/p>\n<p>\u611f\u8c22\u60a8\u7684\u65f6\u95f4\u548c\u5174\u8da3\uff0c \u8fc8\u514b<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u4f60\u53ef\u4ee5\u5b9e\u73b0bson\u63a5\u53e3<\/p>\n<pre>\/\/ bson\/marshal.go \n\n\/\/ marshaler is an interface implemented by types that can marshal themselves\n\/\/ into a bson document represented as bytes. the bytes returned must be a valid\n\/\/ bson document if the error is nil.\ntype marshaler interface {\n    marshalbson() ([]byte, error)\n}\n<\/pre>\n<pre>\/\/ bson\/unmarshal.go\n\n\/\/ Unmarshaler is an interface implemented by types that can unmarshal a BSON\n\/\/ document representation of themselves. The BSON bytes can be assumed to be\n\/\/ valid. UnmarshalBSON must copy the BSON bytes if it wishes to retain the data\n\/\/ after returning.\ntype Unmarshaler interface {\n    UnmarshalBSON([]byte) error\n}\n<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u5c06 MongoDB \u6587\u6863\u4e2d\u7684\u5b57\u7b26\u4e32\u89e3\u7801\u4e3a Go \u4e2d\u7684\u81ea\u5b9a\u4e49\u4e1a\u52a1\u7ed3\u6784\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/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-207932","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207932","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=207932"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207932\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}