{"id":207196,"date":"2025-07-08T09:43:13","date_gmt":"2025-07-08T01:43:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207196\/"},"modified":"2025-07-08T09:43:13","modified_gmt":"2025-07-08T01:43:13","slug":"%e5%a6%82%e4%bd%95%e5%b0%86-bson-%e6%96%87%e6%a1%a3%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%98%a0%e5%b0%84%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207196\/","title":{"rendered":"\u5982\u4f55\u5c06 BSON \u6587\u6863\u8f6c\u6362\u4e3a\u6620\u5c04\u63a5\u53e3{}"},"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 BSON \u6587\u6863\u8f6c\u6362\u4e3a\u6620\u5c04\u63a5\u53e3{}<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 19:09:51<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5982\u4f55\u5c06 BSON \u6587\u6863\u8f6c\u6362\u4e3a\u6620\u5c04\u63a5\u53e3{}\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u8bd5\u56fe\u5c06\u5149\u6807\u7684\u6570\u636e\u89e3\u7801\u4e3amap[string]interface{}\uff0c\u6211\u76f4\u63a5\u5c1d\u8bd5\u4e86\u4f46\u5b83\u6839\u672c\u4e0d\u8d77\u4f5c\u7528\uff0c\u6240\u4ee5\u6211\u53d1\u73b0\u6211\u5fc5\u987b\u5c06\u5176\u8f6c\u6362\u4e3abson\u6587\u6863\u5e76\u4e14\u63a5\u4e0b\u6765\u5c06\u5176\u8f6c\u6362\u4e3amap[string]interface{}\uff0c\u6700\u540e\u8f6c\u6362\u4e3ajson\u5b57\u7b26\u4e32\u3002\u6211\u5c1d\u8bd5\u4e86\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>...\nfor cursor.Next(context.Background()) {\n    err = cursor.Decode(&amp;itemBson)\n    ...\n    b, err := bson.Marshal(itemBson)\n    ...\n    err = bson.Unmarshal(b, &amp;itemMap)\n    ...\n}\n...<\/pre>\n<p>\u4f46\u662fbson\u6587\u6863\u6709\u4ee5\u4e0b\u503c\uff1a<\/p>\n<p>map[_id:objectid(&#8220;5c2d0809a49bad7d547ec028&#8221;) applications:bson.array[bson.document{bson.element{&#8220;enabled&#8221;: true}}] username:coto useruuid:df2d ea92-c189-53b3-aafe-485d0be23bee]<\/p>\n<p>\u5730\u56fe\u89e3\u6790\u4e3a json\uff1a<\/p>\n<p>{&#8220;_id&#8221;:&#8221;5c2d0809a49bad7d547ec028&#8243;,&#8221;applications&#8221;:[{}],&#8221;username&#8221;:&#8221;coto&#8221;,&#8221;useruuid&#8221;:&#8221;df2dea92-c189-53b3-aafe-485d0be23bee&#8221;}<\/p>\n<p>\u6b63\u5982\u60a8\u6240\u770b\u5230\u7684\uff0cjson \u4e2d\u7684\u952e\u201capplications\u201d\u4e3a\u7a7a\uff0c\u4f46 bson \u6587\u6863\u4e2d\u786e\u5b9e\u6709\u5185\u5bb9\u3002\u6211\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\u6570\u636e\u6d88\u5931\u4e86\u3002<\/p>\n<p>\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u9519\u8bef\uff1f \u8c22\u8c22\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p><strong>\u5df2\u89e3\u51b3\uff1a<\/strong><\/p>\n<p>\u6211\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u89e3\u51b3\u4e86\u6b64\u9519\u8bef\uff1a<\/p>\n<pre>var jsondocuments []map[string]interface{}\nvar bytedocuments []byte\n\nvar bsondocument bson.d\nvar jsondocument map[string]interface{}\nvar temporarybytes []byte\n\nfor cursor.next(context.background()) {\n    err = cursor.decode(&amp;bsondocument)\n\n    ...\n\n    temporarybytes, err = bson.marshalextjson(bsondocument, true, true)\n\n    ...\n\n    err = json.unmarshal(temporarybytes, &amp;jsondocument)\n\n    ...\n\n    jsondocuments = append(jsondocuments, jsondocument)\n}<\/pre>\n<pre>temp := itemBson.data.(primitive.D) \/\/ convert interface to primitive D\n\nmetadata := temp.Map() \/\/ map to map[string]interface{}\n\nif v, ok := metadata[prqKey]; ok { \/\/ check and use value\n    commitID = v.(string)\n}<\/pre>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>primitive.d<\/code> \u7c7b\u578b\u4e0a\u7684\u5185\u7f6e\u63a5\u53e3\u5c06\u5176\u8f6c\u6362\u4e3a <code>map[string]\u63a5\u53e3{}<\/code><\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5c06 BSON \u6587\u6863\u8f6c\u6362\u4e3a\u6620\u5c04\u63a5\u53e3{}\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\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-207196","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207196","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=207196"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207196\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}