{"id":207498,"date":"2025-07-08T11:33:29","date_gmt":"2025-07-08T03:33:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207498\/"},"modified":"2025-07-08T11:33:29","modified_gmt":"2025-07-08T03:33:29","slug":"golang%e4%b8%ad%e7%9a%84%e8%81%9a%e5%90%88%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207498\/","title":{"rendered":"golang\u4e2d\u7684\u805a\u5408\u67e5\u8be2"},"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>golang\u4e2d\u7684\u805a\u5408\u67e5\u8be2<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 08:42:32<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300agolang\u4e2d\u7684\u805a\u5408\u67e5\u8be2\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u9700\u8981\u7528 mgo \u5728 golang \u4e2d\u7f16\u5199\u4e00\u4e2a\u67e5\u8be2\uff0c\u8fd9\u662f mongo \u4e2d\u7684\u67e5\u8be2\uff1a<\/p>\n<pre>db.some.aggregate([\n{$match: { \"data.id\": \"11111\" }},\n    {$project: { \n            _id : 0,\n            url : { $concat: [ \"https:\/\/www.someurl.com\/\",\"$data.aID\" ]},\n       items :{\n       \"$map\":{\n                \"input\":\"$data.items\",\n                \"as\":\"it\",\n                \"in\":{\n                  \"item_title\":\"$$it.title\",                             \n                  \"item_id\":\"$$it.id\"\n               }}}}])<\/pre>\n<p>\u5982\u679c\u60a8\u80fd\u5e2e\u52a9\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u5c06\u4e0d\u80dc\u611f\u6fc0\u3002<\/p>\n<p>\/\/ \u5bf9\u4e8e\u9519\u8bef\u63d0\u51fa\u7684\u95ee\u9898\uff0c\u6211\u6df1\u8868\u6b49\u610f\uff0c\u4ee5\u540e\u6211\u4f1a\u6309\u7167\u8bf4\u660e\u8fdb\u884c\u64cd\u4f5c\uff0c\u6211\u7684\u95ee\u9898\u662f $concat<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u91cc\u7684\u8bed\u6cd5\u5f88\u68d8\u624b\uff0c\u56e0\u4e3a\u5b83\u4e0e json \u7c7b\u4f3c\uff0c\u4f46\u53c8\u4e0d\u540c\uff0c\u8db3\u4ee5\u4ee4\u4eba\u56f0\u60d1\u3002<\/p>\n<p>\u7279\u522b\u8bf7\u6ce8\u610f\uff0c\u6570\u7ec4\u4f7f\u7528\u5927\u62ec\u53f7\u5206\u9694\u7b26 <code>[]bson.m{...}<\/code> \u5e76\u4e14 <code>$concat<\/code> \u503c\u662f\u5b57\u7b26\u4e32\u6570\u7ec4 <code>[]string{...}<\/code>\uff1a<\/p>\n<pre>query := []bson.M{ \/\/ NOTE: slice of bson.M here\n  bson.M{\n    \"$match\": bson.M{\"data.id\": \"11111\"},\n  },\n  bson.M{\n    \"$project\": bson.M{\n      \"_id\": 0,\n      \"url\": bson.M{\n        \"$concat\": []string{ \/\/ NOTE: slice of strings here\n          \"https:\/\/www.someurl.com\/\",\n          \"$data.aID\",\n        },\n      },\n      \"items\": bson.M{\n        \"$map\": bson.M{\n          \"input\": \"$data.items\",\n          \"as\":    \"it\",\n          \"in\":    bson.M{\"item_title\": \"$$it.title\", \"item_id\": \"$$it.id\"},\n        },\n      },\n    },\n  },\n}<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/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-207498","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207498","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=207498"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207498\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}