{"id":42980,"date":"2024-12-01T14:29:01","date_gmt":"2024-12-01T06:29:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42980\/"},"modified":"2024-12-01T14:29:01","modified_gmt":"2024-12-01T06:29:01","slug":"%e4%bb%8e-mongodb-%e8%8e%b7%e5%8f%96%e4%b8%a4%e4%b8%aa%e5%9c%b0%e7%90%86%e4%bd%8d%e7%bd%ae%e4%b9%8b%e9%97%b4%e7%9a%84%e8%b7%9d%e7%a6%bb","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42980\/","title":{"rendered":"\u4ece MongoDB \u83b7\u53d6\u4e24\u4e2a\u5730\u7406\u4f4d\u7f6e\u4e4b\u95f4\u7684\u8ddd\u79bb"},"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>\u4ece MongoDB \u83b7\u53d6\u4e24\u4e2a\u5730\u7406\u4f4d\u7f6e\u4e4b\u95f4\u7684\u8ddd\u79bb<\/span><\/p>\n<h1>\u4ece MongoDB \u83b7\u53d6\u4e24\u4e2a\u5730\u7406\u4f4d\u7f6e\u4e4b\u95f4\u7684\u8ddd\u79bb<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 23:51:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60Golang\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u4ece MongoDB \u83b7\u53d6\u4e24\u4e2a\u5730\u7406\u4f4d\u7f6e\u4e4b\u95f4\u7684\u8ddd\u79bb\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u4f7f\u7528 go \u4ece mongodb \u83b7\u53d6 2dsphere \u4e0a\u4e24\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002<\/p>\n<p>\u6211\u6309\u7167\u8fd9\u4e2a\u7b54\u6848\u5c1d\u8bd5\u4e86\u8fd9\u4e2a<\/p>\n<pre>conditions[\"geolocation\"] = bson.M{\n        \"$geoNear\": bson.M{\n            \"near\": bson.M{\n                \"type\":        \"Point\",\n                \"coordinates\": []float64{latitude, longitude},\n            },\n            \"maxDistance\":   rangeInMeters,\n            \"spherical\":     true,\n            \"distanceField\": \"distance\",\n        },\n    }\n\nfilterCursor, err := collection.Find(ctx, conditions)<\/pre>\n<p>\u4f46\u6211\u6536\u5230\u6b64\u9519\u8bef\uff1a<strong>\u201c\u9644\u8fd1\u5730\u7406\u67e5\u8be2\u4e2d\u7684\u65e0\u6548\u4e89\u8bba\uff1anear\u201d<\/strong><\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p> \u4f7f\u7528 mongodb  \u51fd\u6570\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 golang \u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>stages := mongo.Pipeline{}\n    getNearbyStage := bson.D{{\"$geoNear\", bson.M{\n        \"near\": bson.M{\n            \"type\":        \"Point\",\n            \"coordinates\": []float64{longitude, latitude},\n        },\n        \"maxDistance\":   rangeInMeters,\n        \"spherical\":     true,\n        \"distanceField\": \"distance\",\n    }}}\n    stages = append(stages, getNearbyStage)\n\n    filterCursor, err := collection.Aggregate(ctx, stages)\n\n    if err != nil {\n        log.Println(err)\n        return nil, err\n    }<\/pre>\n<p>\u5982\u679c\u60a8\u60f3\u5411\u7ba1\u9053\u6dfb\u52a0\u53e6\u4e00\u4e2a\u67e5\u8be2\uff0c\u60a8\u53ef\u4ee5\u7b80\u5355\u5730\u521b\u5efa\u53e6\u4e00\u4e2a\u9636\u6bb5\u5e76\u5c06\u5176\u9644\u52a0\u5230\u9636\u6bb5\u5207\u7247<\/p>\n<p>\u53e6\u8bf7\u67e5\u770b\u6709\u5173\u5982\u4f55\u5728 golang \u548c mongo \u4e2d\u4f7f\u7528\u805a\u5408\u7ba1\u9053\u7684\u5feb\u901f\u6307\u5357 <\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u4ece MongoDB \u83b7\u53d6\u4e24\u4e2a\u5730\u7406\u4f4d\u7f6e\u4e4b\u95f4\u7684\u8ddd\u79bb \u4ece MongoDB \u83b7\u53d6\u4e24\u4e2a\u5730\u7406\u4f4d\u7f6e\u4e4b\u95f4\u7684\u8ddd\u79bb \u6765\u6e90\uff1astackoverflow 2024-04-27 23:51:35 0\u6d4f\u89c8 \u6536\u85cf \u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60Golang\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u4ece MongoDB \u83b7\u53d6\u4e24\u4e2a\u5730\u7406\u4f4d\u7f6e\u4e4b\u95f4\u7684\u8ddd\u79bb\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u95ee\u9898\u5185\u5bb9 \u6211\u6b63\u5728\u5c1d\u8bd5\u4f7f\u7528 go \u4ece mongodb \u83b7\u53d6 2dsphere \u4e0a\u4e24\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002 \u6211\u6309\u7167\u8fd9\u4e2a\u7b54\u6848\u5c1d\u8bd5\u4e86\u8fd9\u4e2a conditions[&#8220;geolocation&#8221;] = bson.M{ &#8220;$geoNear&#8221;: bson.M{ &#8220;near&#8221;: bson.M{ &#8220;type&#8221;: &#8220;Point&#8221;, &#8220;coordinates&#8221;: []float64{latitude, longitude}, }, &#8220;maxDistance&#8221;: rangeInMeters, &#8220;spherical&#8221;: true, &#8220;distanceField&#8221;: &#8220;distance&#8221;, }, } filterCursor, err := collection.Find(ctx, conditions) \u4f46\u6211\u6536\u5230\u6b64\u9519\u8bef\uff1a\u201c\u9644\u8fd1\u5730\u7406\u67e5\u8be2\u4e2d\u7684\u65e0\u6548\u4e89\u8bba\uff1anear\u201d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42980","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42980","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42980"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42980\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}