{"id":208129,"date":"2025-07-08T15:47:09","date_gmt":"2025-07-08T07:47:09","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208129\/"},"modified":"2025-07-08T15:47:09","modified_gmt":"2025-07-08T07:47:09","slug":"%e4%b8%8e-mongodb-go-%e5%ae%a2%e6%88%b7%e7%ab%af%e4%b8%80%e8%b5%b7%e4%bd%bf%e7%94%a8%e6%97%b6-bson-struct-%e6%a0%87%e7%ad%be%e7%9a%84%e5%bf%85%e8%a6%81%e6%80%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208129\/","title":{"rendered":"\u4e0e MongoDB go \u5ba2\u6237\u7aef\u4e00\u8d77\u4f7f\u7528\u65f6 bson struct \u6807\u7b7e\u7684\u5fc5\u8981\u6027"},"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>\u4e0e MongoDB go \u5ba2\u6237\u7aef\u4e00\u8d77\u4f7f\u7528\u65f6 bson struct \u6807\u7b7e\u7684\u5fc5\u8981\u6027<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 12:06:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e0e MongoDB go \u5ba2\u6237\u7aef\u4e00\u8d77\u4f7f\u7528\u65f6 bson struct \u6807\u7b7e\u7684\u5fc5\u8981\u6027\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u89c2\u770b\u4e00\u7bc7\u5173\u4e8e\u5982\u4f55\u521b\u5efa\u4f7f\u7528 mongodb \u8fdb\u884c\u6301\u4e45\u5316\u7684 go restful api \u7684\u6559\u7a0b\uff08\u66f4\u51c6\u786e\u5730\u8bf4\u662f\u8fd9\u4e2a\uff09\u3002<\/p>\n<p>\u8bb2\u5e08\u5728\u4ed6\u7684\u6a21\u578b\uff08\u7ed3\u6784\uff09\u4e2d\u4f7f\u7528<strong>\u4e24\u8005<\/strong> <code>json<\/code> \u548c <code>bson<\/code> \u6807\u7b7e\uff0c\u7c7b\u4f3c\u4e8e<\/p>\n<pre>type NoteUpdate struct {\n    ID        string `json:\"id,omitempty\" bson:\"_id,omitempty\"`\n    Title     string `json:\"title\" bson:\"title,omitempty\"`\n    Content   string `json:\"content\" bson:\"content,omitempty\"`\n    ChangedAt int64  `json:\"changed_at\" bson:\"changed_at\"`\n}<\/pre>\n<p>\u4f46\u662f\u5b98\u65b9\u7684 go \u9a71\u52a8\u7a0b\u5e8f\u793a\u4f8b\u5e76\u6ca1\u6709\u8fd9\u6837\u505a\u3002<\/p>\n<p>\u4e8b\u5b9e\u4e0a\uff0c\u6839\u672c\u6ca1\u6709\u4f7f\u7528\u4efb\u4f55\u7ed3\u6784\u6807\u7b7e\u3002<\/p>\n<p>\u4f7f\u7528 <code>bson<\/code> \u6807\u7b7e\u7684\u76ee\u7684\/\u7528\u5904\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u6211\u60f3\u5230\u7684\u4e00\u4ef6\u4e8b\u662f\uff0c\u4eba\u4eec\u60f3\u8981\u521b\u5efa\u81ea\u5b9a\u4e49 mongo <code>_id<\/code> \u5b57\u6bb5\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5e94\u8be5\u58f0\u660e\u4e0e\u8be5\u7ed3\u6784\u4f53\u5b57\u6bb5\u7684\u663e\u5f0f <code>bson<\/code> \u6620\u5c04\u3002<\/p>\n<p><code>bson<\/code> \u6807\u7b7e\u8fd8\u6709\u5176\u4ed6\u9644\u52a0\u503c\u5417\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>mongodb \u9a71\u52a8\u7a0b\u5e8f\u4ec5\u4f7f\u7528 <code>bson<\/code> \u6807\u7b7e\u3002 <code>json<\/code> \u6807\u7b7e\u4ec5\u9002\u7528\u4e8e <code>encoding\/json<\/code> \u5305\uff08\u6216\u5904\u7406 json \u7f16\u7ec4\/\u89e3\u7ec4\u7684\u5176\u4ed6 3rd \u65b9\u5305\uff09\u3002<\/p>\n<p>\u60a8\u4e0d\u9700\u8981\u6307\u5b9a\u548c\u4f7f\u7528 <code>bson<\/code> \u6807\u8bb0\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u9a71\u52a8\u7a0b\u5e8f\u5728\u7f16\u7801\u7ed3\u6784\u503c\u65f6\u901a\u5e38\u53ea\u4f7f\u7528\u5c0f\u5199\u5b57\u6bb5\u540d\u79f0\u3002\u4f46\u662f\uff0c\u5f53\u60a8\u9700\u8981\u4e0d\u540c\u7684\u540d\u79f0\u65f6\uff0c\u9700\u8981 <code>bson<\/code> \u6807\u7b7e\u3002<\/p>\n<p>\u5373\u4f7f\u60a8\u60f3\u4f7f\u7528\u5c0f\u5199\u7684\u5b57\u6bb5\u540d\u79f0\uff0c\u6307\u5b9a <code>bson<\/code> \u6807\u8bb0\u4e5f\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\uff0c\u56e0\u4e3a\u6709\u65f6\u60a8\u53ef\u80fd\u9700\u8981\u91cd\u547d\u540d\u7ed3\u6784\u5b57\u6bb5\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u9ebb\u70e6\u548c\u4e0d\u4e00\u81f4\u3002\u5982\u679c\u60a8\u6307\u5b9a <code>bson<\/code> \u6807\u8bb0\uff0c\u90a3\u4e48\u5c06\u6765\u91cd\u547d\u540d\u5b57\u6bb5\u4e5f\u6ca1\u5173\u7cfb\uff0c\u5b83\u4eec\u4ecd\u7136\u4f1a\u7f16\u7ec4\u5230\u76f8\u540c\u7684\u5c5e\u6027\u4e2d\uff0c\u5e76\u4e14\u89e3\u7ec4\u5b83\u4eec\u5c06\u7ee7\u7eed\u5de5\u4f5c\u3002<\/p>\n<p>bson \u6807\u8bb0\u8fd8\u53ef\u4ee5\u5305\u542b \u66f4\u6539\u9ed8\u8ba4\u5c01\u9001\u884c\u4e3a\uff1a<\/p>\n<pre>OmitEmpty  Only include the field if it's not set to the zero value for the type or to\n           empty slices or maps.\n\nMinSize    Marshal an integer of a type larger than 32 bits value as an int32, if that's\n           feasible while preserving the numeric value.\n\nTruncate   When unmarshaling a BSON double, it is permitted to lose precision to fit within\n           a float32.\n\nInline     Inline the field, which must be a struct or a map, causing all of its fields\n           or keys to be processed as if they were part of the outer struct. For maps,\n           keys must not conflict with the bson keys of other struct fields.\n\nSkip       This struct field should be skipped. This is usually denoted by parsing a \"-\"\n           for the name.<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u4e0e MongoDB go \u5ba2\u6237\u7aef\u4e00\u8d77\u4f7f\u7528\u65f6 bson struct \u6807\u7b7e\u7684\u5fc5\u8981\u6027\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\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-208129","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208129","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=208129"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208129\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}