{"id":207278,"date":"2025-07-08T10:24:29","date_gmt":"2025-07-08T02:24:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207278\/"},"modified":"2025-07-08T10:24:29","modified_gmt":"2025-07-08T02:24:29","slug":"mongodb-go-%e9%a9%b1%e5%8a%a8%e7%a8%8b%e5%ba%8f%e6%97%a0%e6%b3%95%e6%ad%a3%e7%a1%ae%e8%a7%a3%e7%bb%84%e5%b5%8c%e5%a5%97%e6%96%87%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207278\/","title":{"rendered":"MongoDB Go \u9a71\u52a8\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u786e\u89e3\u7ec4\u5d4c\u5957\u6587\u6863"},"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>MongoDB Go \u9a71\u52a8\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u786e\u89e3\u7ec4\u5d4c\u5957\u6587\u6863<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 12:45:41<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aMongoDB Go \u9a71\u52a8\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u786e\u89e3\u7ec4\u5d4c\u5957\u6587\u6863\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u5bf9\u96c6\u5408\u4e2d\u7684\u7279\u5b9a\u5b57\u6bb5\u8fdb\u884c\u64cd\u4f5c\u7684 setter \u548c getter\u3002 setter \u5de5\u4f5c\u6b63\u5e38\u5e76\u4e14\u6587\u6863\u6309\u9884\u671f\u66f4\u65b0\uff0c\u4f46\u662f getter \u65e0\u6cd5\u6b63\u786e\u8fd4\u56de\u586b\u5145\u7684\u7ed3\u6784\u3002 <strong>\u6211\u505a\u9519\u4e86\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p><strong>\u4f5c\u4e3a go \u7ed3\u6784\u7684\u96c6\u5408<\/strong>\uff1a<\/p>\n<pre>\ntype model struct {\n    id         primitive.objectid `bson:\"_id,omitempty\"`\n    entitytype string             `bson:\"entity_type,omitempty\"`\n    entityid   string             `bson:\"entity_id,omitempty\"`\n    configsource configsources `bson:\"config_source,inline,omitempty\"`\n}\n\ntype configsources struct {\n    configs []configsource `bson:\"configs,omitempty\"`\n}\n\ntype configsource struct {\n    hour   int    `bson:\"hour\"`\n    source string `bson:\"source\"`\n}\n\n<\/pre>\n<p><strong>\u8bbe\u7f6e\u5668\u7247\u6bb5\uff1a<\/strong><\/p>\n<pre>cfg := configsources{\n    configs: []configsource{\n        {\n            hour: 1,\n            source: \"hour_1_source\",\n        },\n        {\n            hour: 2,\n            source: \"hour_2_source\",\n        },\n    },\n}\nc := db.collection(\"foo\")\nselectorquery := bson.m{\"entity_id\": entityid}\nupdatequery := bson.m{\"$set\": bson.m{\"config_source\": configname}}\nresult, err := c.updatemany(ctx, selectorquery, updatequery)\n<\/pre>\n<p><strong>getter \u4ee3\u7801\u7247\u6bb5\uff1a<\/strong><\/p>\n<pre>c := db.collection(\"foo\")\nq, err := c.find(ctx, bson.m{\"_id\": bson.m{\"$in\": idsimquerying}})\nif err != nil {\n    return nil\n}\nvar results []model\nerr = q.all(ctx, &amp;results)\nfmt.printf(\"\\n\\n\\n\\n%#v\\n\\n\\n\\n\", results) \/\/ this output is shown below\n<\/pre>\n<p><strong>\u5f97\u5230\u7684\u7ed3\u679c\uff1a<\/strong><\/p>\n<pre>[]Model{\n    Model{\n        ID:primitive.ObjectID{0x5a, 0xa9, 0x7a, 0x40, 0xdf, 0xe5, 0x90, 0x44, 0x49, 0xdb, 0x61, 0x4},\n        EntityType:\"CELL\",\n        EntityID:\"4110902605985611776\",\n        ConfigSource:ConfigSources{\n            Configs:[]ConfigSource(nil)\n        }\n    }\n}<\/pre>\n<p><strong>atlas \u4e2d\u67e5\u770b\u7684\u5b57\u6bb5\uff1a<\/strong><\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u4e00\u65e6\u6211\u4ece\u6a21\u578b\u7ed3\u6784\u4e2d\u5220\u9664\u5185\u8054\uff0c\u5b83\u5c31\u5de5\u4f5c\u6b63\u5e38<\/p>\n<pre>type Model struct {\n        ID         primitive.ObjectID `bson:\"_id,omitempty\"`\n        EntityType string             `bson:\"entity_type,omitempty\"`\n        EntityID   string             `bson:\"entity_id,omitempty\"`\n        ConfigSource ConfigSources `bson:\"config_source,omitempty\"`\n    }<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aMongoDB Go \u9a71\u52a8\u7a0b\u5e8f\u65e0\u6cd5\u6b63\u786e\u89e3\u7ec4\u5d4c\u5957\u6587\u6863\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\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-207278","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207278","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=207278"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207278\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}