{"id":3787,"date":"2019-01-24T14:15:21","date_gmt":"2019-01-24T06:15:21","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3787"},"modified":"2019-01-24T14:15:21","modified_gmt":"2019-01-24T06:15:21","slug":"mongodb%e4%b8%ad%e5%a4%9a%e8%a1%a8%e5%85%b3%e8%81%94%e6%9f%a5%e8%af%a2join%e6%93%8d%e4%bd%9clookup%e4%bd%bf%e7%94%a8%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3787","title":{"rendered":"MongoDB\u4e2d\u591a\u8868\u5173\u8054\u67e5\u8be2JOIN\u64cd\u4f5c$lookup\u4f7f\u7528\u6559\u7a0b"},"content":{"rendered":"<p>\n\tMongoDB \u662f\u4e00\u4e2a NoSQL \u6570\u636e\u5e93\uff0cNoSQL \u7684\u6700\u5927\u7f3a\u70b9\u5c31\u662f Join \u67e5\u8be2\u4e0d\u597d\u64cd\u4f5c\u3002NoSQL \u5e76\u4e0d\u662f\u8bf4\u5b83\u4e0d\u662f\u4e00\u4e2a SQL\uff0c\u800c\u662f\u4e00\u4e2a not only sql\uff0c\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a SQL\u3002\u90a3\u4e48\u5728 Mongo \u4e2d\u5982\u4f55\u8fdb\u884c\u591a\u8868 Join \u67e5\u8be2\u5462\uff1f\u4eca\u5929\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u5b66\u4e60\u5b66\u4e60\uff01\n<\/p>\n<p>\n\t\u5728 MongoDB \u4e2d\uff0c\u591a\u8868\u5173\u8054\u67e5\u8be2\uff0c\u6709\u591a\u79cd\u65b9\u5f0f\u3002\u4eca\u5929\u6211\u4eec\u5148\u6765\u5b66\u4e60 $lookup \u7684\u4f7f\u7528\uff0c\u5b83\u652f\u6301\u4e24\u8868\u4ee5\u4e0a\uff0c3 \u8868\uff08\u96c6\u5408\uff09\u7b49\u90fd\u53ef\u4ee5\uff0c$lookup \u76f8\u5f53\u4e8e\u5de6\u8fde\u63a5\u67e5\u8be2\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"$lookup\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1fzhn79qygqj20fw0cl0tm.jpg\" style=\"width: 572px;height: 453px\" \/>\n<\/p>\n<p>\n\tNoSQL \uff01= No JOIN\u3002NoSQL \u5e76\u4e0d\u4ee3\u8868\u4e0d\u80fd JOIN \u64cd\u4f5c\u3002\n<\/p>\n<p>\n\t\u6211\u6709\u4e00\u4e2a order \u96c6\u5408\uff0c\u8fd8\u6709\u4e00\u4e2a user \u96c6\u5408\uff0c\u8fd8\u6709\u4e00\u4e2a product \u96c6\u5408\u3002\u6216\u8005\u628a\u5b83\u4eec\u79f0\u4e3a\u8868\uff0c\u90fd\u53ef\u4ee5\uff0c\u53eb\u6cd5\u968f\u4fbf\u3002\u4e0b\u9762\u662f\u6211\u4e3a\u4e86\u8bb2\u6e05\u695a $lookup \u7684\u7528\u6cd5\uff0c\u6240\u7528\u7684\u6d4b\u8bd5\u6570\u636e\uff01\n<\/p>\n<pre class=\"brush:jscript;\">\r\ndb.product.insert({&quot;_id&quot;:1,&quot;productname&quot;:&quot;\u5546\u54c11&quot;,&quot;price&quot;:15});\r\ndb.product.insert({&quot;_id&quot;:2,&quot;productname&quot;:&quot;\u5546\u54c12&quot;,&quot;price&quot;:36});\r\n\r\ndb.order.insert({&quot;_id&quot;:1,&quot;pid&quot;:1,&quot;ordername&quot;:&quot;\u8ba2\u53551&quot;,&quot;uid&quot;:1});\r\ndb.order.insert({&quot;_id&quot;:2,&quot;pid&quot;:2,&quot;ordername&quot;:&quot;\u8ba2\u53552&quot;,&quot;uid&quot;:2});\r\ndb.order.insert({&quot;_id&quot;:3,&quot;pid&quot;:2,&quot;ordername&quot;:&quot;\u8ba2\u53553&quot;,&quot;uid&quot;:2});\r\ndb.order.insert({&quot;_id&quot;:4,&quot;pid&quot;:1,&quot;ordername&quot;:&quot;\u8ba2\u53554&quot;,&quot;uid&quot;:1});\r\n\r\ndb.user.insert({&quot;_id&quot;:1,&quot;username&quot;:1});\r\ndb.user.insert({&quot;_id&quot;:2,&quot;username&quot;:2});\r\n\r\ndb.product.find();\r\ndb.order.find();\r\ndb.user.find();<\/pre>\n<h2>\n\t$lookup \u4e24\u8868\u5173\u8054join<br \/>\n<\/h2>\n<p>\n\t\u770b\u4e0b\u9762\u7684\u67e5\u8be2\u8bed\u53e5\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\ndb.product.aggregate([\r\n    {\r\n      $lookup:\r\n        {\r\n          from: &quot;order&quot;,\r\n          localField: &quot;_id&quot;,\r\n          foreignField: &quot;pid&quot;,\r\n          as: &quot;inventory_docs&quot;\r\n        }\r\n   }\r\n]);<\/pre>\n<p>\n\t\u6267\u884c\u7684\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff0c\u53ea\u5217\u4e86\u4e00\u6761\u6570\u636e\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\n{\r\n    &quot;_id&quot; : 1.0,\r\n    &quot;productname&quot; : &quot;\u5546\u54c11&quot;,\r\n    &quot;price&quot; : 15.0,\r\n    &quot;inventory_docs&quot; : [ \r\n        {\r\n            &quot;_id&quot; : 1.0,\r\n            &quot;pid&quot; : 1.0,\r\n            &quot;ordername&quot; : &quot;\u8ba2\u53551&quot;\r\n        }, \r\n        {\r\n            &quot;_id&quot; : 4.0,\r\n            &quot;pid&quot; : 1.0,\r\n            &quot;ordername&quot; : &quot;\u8ba2\u53554&quot;\r\n        }\r\n    ]\r\n}<\/pre>\n<p>\n\tlookup \u5c31\u662f\u4f7f\u7528 aggregate \u7684 $lookup \u5c5e\u6027\uff0c$lookup \u64cd\u4f5c\u9700\u8981\u4e00\u4e2a\u56db\u4e2a\u53c2\u6570\u7684\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u7684\u5c5e\u6027\u89e3\u91ca\u5982\u4e0b\uff1a\n<\/p>\n<ul>\n<li>\n\t\tlocalField\uff1a\u5728\u8f93\u5165\u6587\u6863\u4e2d\u7684\u67e5\u627e\u5b57\u6bb5\n\t<\/li>\n<li>\n\t\tfrom\uff1a\u9700\u8981\u8fde\u63a5\u7684\u96c6\u5408\n\t<\/li>\n<li>\n\t\tforeignField\uff1a\u9700\u8981\u5728from\u96c6\u5408\u4e2d\u67e5\u627e\u7684\u5b57\u6bb5\n\t<\/li>\n<li>\n\t\tas\uff1a\u8f93\u51fa\u7684\u5b57\u6bb5\u540d\u5b57\n\t<\/li>\n<\/ul>\n<p>\n\t\u5728\u8f93\u51fa\u7684\u7ed3\u679c\u4e2d\uff0c\u4f1a\u5305\u542b\u4e00\u4e2a inventory_docs \u7684\u5b57\u6bb5\uff0c\u5b83\u4f1a\u628a order \u4e2d\u6240\u5173\u8054\u7684\u6570\u636e\u5728\u6570\u7ec4\u4e2d\u5c55\u73b0\u3002\n<\/p>\n<h2>\n\t$lookup \u4e09\u8868\u5173\u8054join<br \/>\n<\/h2>\n<p>\n\t$lookup \u7684\u4e09\u8868\u5173\u8054\u67e5\u8be2\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u8981\u914d\u7f6e\u4e24\u4e2a $lookup \u5373\u53ef\u3002\u5177\u4f53\u67e5\u8be2\u4ee3\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\ndb.order.aggregate([{\r\n    $lookup:\r\n    {\r\n      from: &quot;product&quot;,\r\n      localField: &quot;pid&quot;,\r\n      foreignField: &quot;_id&quot;,\r\n      as: &quot;inventory_docs&quot;\r\n    }\r\n},{\r\n    $lookup:\r\n    {\r\n      from: &quot;user&quot;,\r\n      localField: &quot;uid&quot;,\r\n      foreignField: &quot;_id&quot;,\r\n      as: &quot;user_docs&quot;\r\n    }\r\n}]);<\/pre>\n<p>\n\t\u5176\u5b9e\u9664\u4e86 $lookup\uff0cpopulate \u4e5f\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u5173\u8054\u67e5\u8be2\uff0c\u8fd9\u4e2a populate \u7684\u7528\u6cd5\uff0c\u6211\u4eec\u540e\u9762\u518d\u4e13\u95e8\u5199\u6587\u7ae0\u6765\u5b66\u4e60\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3787\">MongoDB\u4e2d\u591a\u8868\u5173\u8054\u67e5\u8be2JOIN\u64cd\u4f5c$lookup\u4f7f\u7528\u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>MongoDB \u662f\u4e00\u4e2a NoSQL \u6570\u636e\u5e93\uff0cNoSQL \u7684\u6700\u5927\u7f3a\u70b9\u5c31\u662f Join \u67e5\u8be2\u4e0d\u597d\u64cd\u4f5c\u3002NoSQL \u5e76 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[1905,711,1578,1577,681],"class_list":["post-3787","post","type-post","status-publish","format-standard","hentry","category-yyzt","tag-aggregate","tag-forkjoinpool","tag-mongodb","tag-nosql","tag-681"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3787"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3787\/revisions"}],"predecessor-version":[{"id":3788,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3787\/revisions\/3788"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}