{"id":2477,"date":"2025-03-09T09:20:29","date_gmt":"2025-03-09T01:20:29","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2477"},"modified":"2025-03-09T13:10:34","modified_gmt":"2025-03-09T05:10:34","slug":"mongodb-%e5%85%b3%e7%b3%bb","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/09\/mongodb-%e5%85%b3%e7%b3%bb\/","title":{"rendered":"MongoDB \u5173\u7cfb"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">MongoDB \u662f\u4e00\u4e2a NoSQL \u6570\u636e\u5e93\uff0c\u91c7\u7528\u7684\u662f<strong>\u9762\u5411\u6587\u6863\uff08Document-Oriented\uff09<\/strong>\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u800c\u4e0d\u662f\u4f20\u7edf\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08RDBMS\uff09\u7684\u8868\u7ed3\u6784\u3002\u56e0\u6b64\uff0c\u5728 MongoDB \u4e2d\uff0c\u5e76\u6ca1\u6709\u4e25\u683c\u610f\u4e49\u4e0a\u7684<strong>\u5173\u7cfb\uff08Relationships\uff09<\/strong>\u6982\u5ff5\uff0c\u4f46\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u67d0\u4e9b\u65b9\u5f0f\u6765\u8868\u793a\u548c\u7ba1\u7406\u6570\u636e\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">MongoDB \u4e2d\u7684\u5173\u7cfb\u7c7b\u578b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 MongoDB \u4e2d\uff0c\u6570\u636e\u7684\u5173\u7cfb\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7<strong>\u5185\u5d4c\u6587\u6863\uff08Embedded Documents\uff09<\/strong>\u6216<strong>\u5f15\u7528\uff08References\uff09<\/strong>\u4e24\u79cd\u65b9\u5f0f\u6765\u8868\u793a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u5185\u5d4c\u6587\u6863\uff08Embedded Documents\uff09<\/strong>\uff08\u5d4c\u5957\u5173\u7cfb\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c06\u76f8\u5173\u7684\u6570\u636e\u5d4c\u5957\u5b58\u50a8\u5728\u4e00\u4e2a\u6587\u6863\u4e2d\uff0c\u9002\u7528\u4e8e<strong>\u4e00\u5bf9\u4e00\uff081:1\uff09<\/strong>\u548c<strong>\u4e00\u5bf9\u591a\uff081:N\uff09<\/strong>\u7684\u5173\u7cfb\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u7528\u6237\uff08User\uff09\u548c\u5730\u5740\uff08Address\uff09<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"_id\": 1,\n  \"name\": \"Alice\",\n  \"email\": \"alice@example.com\",\n  \"address\": {\n    \"street\": \"123 Main St\",\n    \"city\": \"New York\",\n    \"zip\": \"10001\"\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>address<\/code> \u76f4\u63a5\u5b58\u50a8\u5728 <code>User<\/code> \u6587\u6863\u4e2d\uff0c\u65e0\u9700\u989d\u5916\u7684\u67e5\u8be2\u5373\u53ef\u83b7\u53d6\u7528\u6237\u7684\u5730\u5740\u4fe1\u606f\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684<strong>\u4f18\u70b9<\/strong>\u662f\u8bfb\u53d6\u901f\u5ea6\u5feb\uff0c<strong>\u7f3a\u70b9<\/strong>\u662f\u5982\u679c <code>address<\/code> \u9700\u8981\u88ab\u591a\u4e2a\u7528\u6237\u5f15\u7528\uff0c\u6570\u636e\u53ef\u80fd\u4f1a\u5197\u4f59\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u5f15\u7528\uff08References\uff09<\/strong>\uff08\u5916\u952e\u5173\u7cfb\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6587\u6863\u4e4b\u95f4\u4f7f\u7528<strong>\u5f15\u7528 ID<\/strong> \u6765\u5efa\u7acb\u5173\u7cfb\uff0c\u9002\u7528\u4e8e<strong>\u591a\u5bf9\u4e00\uff08N:1\uff09<\/strong>\u548c<strong>\u591a\u5bf9\u591a\uff08N:M\uff09<\/strong>\u7684\u5173\u7cfb\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u7528\u6237\uff08User\uff09\u548c\u8ba2\u5355\uff08Orders\uff09<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"_id\": 1,\n  \"name\": \"Alice\",\n  \"email\": \"alice@example.com\",\n  \"orders\": &#91;101, 102] \n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"_id\": 101,\n  \"user_id\": 1,\n  \"product\": \"Laptop\",\n  \"price\": 1200\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u91cc\uff0c\u7528\u6237 Alice \u7684\u8ba2\u5355\u5b58\u50a8\u5728 <code>orders<\/code> \u96c6\u5408\u4e2d\uff0c\u5e76\u901a\u8fc7 <code>user_id<\/code> \u8fdb\u884c\u5173\u8054\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684<strong>\u4f18\u70b9<\/strong>\u662f\u51cf\u5c11\u6570\u636e\u5197\u4f59\uff0c<strong>\u7f3a\u70b9<\/strong>\u662f\u67e5\u8be2\u65f6\u53ef\u80fd\u9700\u8981\u591a\u6b21 <code>lookup<\/code>\uff0c\u5f71\u54cd\u67e5\u8be2\u6548\u7387\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u9009\u62e9\u54ea\u79cd\u5173\u7cfb\u65b9\u5f0f\uff1f<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u5173\u7cfb\u7d27\u5bc6\uff0c\u67e5\u8be2\u9891\u7e41\uff0c\u8bfb\u64cd\u4f5c\u8f83\u591a<\/strong> \u2192 <strong>\u4f7f\u7528\u5185\u5d4c\u6587\u6863<\/strong><\/li>\n\n\n\n<li><strong>\u6570\u636e\u5173\u7cfb\u677e\u6563\uff0c\u6570\u636e\u5171\u4eab\uff0c\u5199\u64cd\u4f5c\u8f83\u591a<\/strong> \u2192 <strong>\u4f7f\u7528\u5f15\u7528\u65b9\u5f0f<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5728 MongoDB \u4e2d\u6267\u884c\u5173\u7cfb\u67e5\u8be2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MongoDB \u901a\u8fc7 <strong>$lookup<\/strong> \u8fdb\u884c\u7c7b\u4f3c SQL \u7684<strong>\u8054\u8868\u67e5\u8be2\uff08JOIN\uff09<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u4f7f\u7528 <code>$lookup<\/code> \u67e5\u8be2\u7528\u6237\u53ca\u5176\u8ba2\u5355<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>db.users.aggregate(&#91;\n  {\n    $lookup: {\n      from: \"orders\",     \/\/ \u5173\u8054\u7684\u96c6\u5408\n      localField: \"_id\",  \/\/ users \u96c6\u5408\u4e2d\u7684\u5b57\u6bb5\n      foreignField: \"user_id\", \/\/ orders \u96c6\u5408\u4e2d\u7684\u5b57\u6bb5\n      as: \"user_orders\"   \/\/ \u7ed3\u679c\u5b58\u653e\u7684\u5b57\u6bb5\n    }\n  }\n])<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u67e5\u8be2\u4f1a\u8fd4\u56de\u5982\u4e0b\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"_id\": 1,\n  \"name\": \"Alice\",\n  \"email\": \"alice@example.com\",\n  \"user_orders\": &#91;\n    {\n      \"_id\": 101,\n      \"user_id\": 1,\n      \"product\": \"Laptop\",\n      \"price\": 1200\n    }\n  ]\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>MongoDB \u6ca1\u6709\u4f20\u7edf RDBMS \u7684\u5916\u952e\u7ea6\u675f<\/strong>\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u6587\u6863\u6216\u5f15\u7528\u6765\u8868\u793a\u5173\u7cfb\u3002<\/li>\n\n\n\n<li><strong>\u5d4c\u5957\u6587\u6863\u9002\u7528\u4e8e\u4e00\u5bf9\u4e00\u548c\u4e00\u5bf9\u591a\u7684\u60c5\u51b5<\/strong>\uff0c\u67e5\u8be2\u66f4\u9ad8\u6548\uff0c\u4f46\u53ef\u80fd\u4f1a\u4ea7\u751f\u6570\u636e\u5197\u4f59\u3002<\/li>\n\n\n\n<li><strong>\u5f15\u7528\u9002\u7528\u4e8e\u591a\u5bf9\u4e00\u548c\u591a\u5bf9\u591a\u7684\u60c5\u51b5<\/strong>\uff0c\u6570\u636e\u66f4\u89c4\u8303\uff0c\u4f46\u67e5\u8be2\u65f6\u9700\u8981\u989d\u5916\u7684 <code>lookup<\/code> \u5904\u7406\u3002<\/li>\n\n\n\n<li><strong>$lookup<\/strong> \u63d0\u4f9b\u4e86 SQL JOIN \u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u53ef\u7528\u4e8e\u67e5\u8be2\u5f15\u7528\u6570\u636e\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB \u662f\u4e00\u4e2a NoSQL \u6570\u636e\u5e93\uff0c\u91c7\u7528\u7684\u662f\u9762\u5411\u6587\u6863\uff08Document-Oriented\uff09\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2548,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[],"class_list":["post-2477","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2477"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2477\/revisions"}],"predecessor-version":[{"id":2478,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2477\/revisions\/2478"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2548"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}