{"id":180318,"date":"2024-11-09T09:13:19","date_gmt":"2024-11-09T01:13:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/180318\/"},"modified":"2024-11-09T09:13:20","modified_gmt":"2024-11-09T01:13:20","slug":"%e8%a8%ad%e8%a8%88%e5%af%a6%e4%be%8b%e5%b0%8d%e6%af%94%ef%bc%9amysql-vs-mongodb","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/180318\/","title":{"rendered":"\u8a2d\u8a08\u5be6\u4f8b\u5c0d\u6bd4\uff1aMySQL vs MongoDB"},"content":{"rendered":"<h1 id=\"%e8%a8%ad%e8%a8%88%e5%af%a6%e4%be%8b%e5%b0%8d%e6%af%94%ef%bc%9amysql-vs-mongodb-DthnMvQOfD\">\u8a2d\u8a08\u5be6\u4f8b\u5c0d\u6bd4\uff1aMySQL vs MongoDB<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u81f3\u95dc\u91cd\u8981\u3002MySQL\u548cMongoDB\u662f\u5169\u7a2e\u6d41\u884c\u7684\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5404\u81ea\u64c1\u6709\u7368\u7279\u7684\u7279\u6027\u548c\u512a\u52e2\u3002\u672c\u6587\u5c07\u5c0d\u9019\u5169\u8005\u9032\u884c\u6df1\u5165\u6bd4\u8f03\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u6839\u64da\u5177\u9ad4\u9700\u6c42\u505a\u51fa\u660e\u667a\u7684\u9078\u64c7\u3002<\/p>\n<h2 id=\"mysql%e6%a6%82%e8%bf%b0-DthnMvQOfD\">MySQL\u6982\u8ff0<\/h2>\n<p>MySQL\u662f\u4e00\u7a2e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff08RDBMS\uff09\uff0c\u4f7f\u7528\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff08SQL\uff09\u4f86\u7ba1\u7406\u6578\u64da\u3002\u5b83\u4ee5\u5176\u7a69\u5b9a\u6027\u548c\u53ef\u9760\u6027\u800c\u805e\u540d\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7279\u5225\u662f\u9700\u8981\u8907\u96dc\u67e5\u8a62\u548c\u4e8b\u52d9\u652f\u6301\u7684\u5834\u666f\u3002<\/p>\n<h3 id=\"%e7%89%b9%e6%80%a7-DthnMvQOfD\">\u7279\u6027<\/h3>\n<ul>\n<li><strong>\u7d50\u69cb\u5316\u6578\u64da\uff1a<\/strong>MySQL\u4f7f\u7528\u8868\u683c\u4f86\u5b58\u5132\u6578\u64da\uff0c\u6578\u64da\u4e4b\u9593\u7684\u95dc\u4fc2\u901a\u904e\u5916\u9375\u4f86\u7dad\u8b77\u3002<\/li>\n<li><strong>ACID\u652f\u6301\uff1a<\/strong>MySQL\u652f\u6301\u539f\u5b50\u6027\u3001\u4e00\u81f4\u6027\u3001\u9694\u96e2\u6027\u548c\u6301\u4e45\u6027\uff08ACID\uff09\uff0c\u78ba\u4fdd\u6578\u64da\u7684\u5b8c\u6574\u6027\u3002<\/li>\n<li><strong>\u8907\u96dc\u67e5\u8a62\uff1a<\/strong>\u652f\u6301JOIN\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8f15\u9b06\u5730\u5f9e\u591a\u500b\u8868\u4e2d\u6aa2\u7d22\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"mongodb%e6%a6%82%e8%bf%b0-DthnMvQOfD\">MongoDB\u6982\u8ff0<\/h2>\n<p>MongoDB\u662f\u4e00\u7a2e\u975e\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff08NoSQL\uff09\uff0c\u4ee5\u6587\u6a94\u70ba\u57fa\u790e\uff0c\u4f7f\u7528\u985e\u4f3cJSON\u7684\u683c\u5f0f\u4f86\u5b58\u5132\u6578\u64da\u3002\u5b83\u7279\u5225\u9069\u5408\u9700\u8981\u9ad8\u53ef\u64f4\u5c55\u6027\u548c\u9748\u6d3b\u6578\u64da\u6a21\u578b\u7684\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n<h3 id=\"%e7%89%b9%e6%80%a7-DthnMvQOfD\">\u7279\u6027<\/h3>\n<ul>\n<li><strong>\u9748\u6d3b\u7684\u6578\u64da\u6a21\u578b\uff1a<\/strong>MongoDB\u5141\u8a31\u5b58\u5132\u7d50\u69cb\u4e0d\u56fa\u5b9a\u7684\u6578\u64da\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u96a8\u6642\u8abf\u6574\u6578\u64da\u7d50\u69cb\u3002<\/li>\n<li><strong>\u9ad8\u53ef\u64f4\u5c55\u6027\uff1a<\/strong>MongoDB\u652f\u6301\u6c34\u5e73\u64f4\u5c55\uff0c\u80fd\u5920\u8f15\u9b06\u5730\u8655\u7406\u5927\u91cf\u6578\u64da\u3002<\/li>\n<li><strong>\u5feb\u901f\u67e5\u8a62\uff1a<\/strong>\u7531\u65bc\u6578\u64da\u4ee5\u6587\u6a94\u5f62\u5f0f\u5b58\u5132\uff0c\u67e5\u8a62\u901f\u5ea6\u901a\u5e38\u6bd4\u50b3\u7d71\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u66f4\u5feb\u3002<\/li>\n<\/ul>\n<h2 id=\"%e8%a8%ad%e8%a8%88%e5%af%a6%e4%be%8b%e5%b0%8d%e6%af%94-DthnMvQOfD\">\u8a2d\u8a08\u5be6\u4f8b\u5c0d\u6bd4<\/h2>\n<h3 id=\"%e6%95%b8%e6%93%9a%e6%a8%a1%e5%9e%8b-DthnMvQOfD\">\u6578\u64da\u6a21\u578b<\/h3>\n<p>\u5728MySQL\u4e2d\uff0c\u6578\u64da\u4ee5\u8868\u683c\u5f62\u5f0f\u7d44\u7e54\uff0c\u6bcf\u500b\u8868\u90fd\u6709\u56fa\u5b9a\u7684\u5217\u548c\u6578\u64da\u985e\u578b\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u662fMySQL\u4e2d\u4e00\u500b\u7528\u6236\u8868\u7684\u8a2d\u8a08\uff1a<\/p>\n<pre><code>CREATE TABLE Users (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    name VARCHAR(100),\n    email VARCHAR(100),\n    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);<\/code><\/pre>\n<p>\u76f8\u5c0d\u800c\u8a00\uff0cMongoDB\u4f7f\u7528\u6587\u6a94\u4f86\u5b58\u5132\u6578\u64da\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u7d50\u69cb\u66f4\u52a0\u9748\u6d3b\u3002\u4ee5\u4e0b\u662fMongoDB\u4e2d\u76f8\u540c\u7528\u6236\u6578\u64da\u7684\u8a2d\u8a08\uff1a<\/p>\n<pre><code>{\n    \"_id\": ObjectId(\"...\"),\n    \"name\": \"John Doe\",\n    \"email\": \"john@example.com\",\n    \"created_at\": ISODate(\"2023-01-01T00:00:00Z\")\n}<\/code><\/pre>\n<h3 id=\"%e6%9f%a5%e8%a9%a2%e6%80%a7%e8%83%bd-DthnMvQOfD\">\u67e5\u8a62\u6027\u80fd<\/h3>\n<p>\u5728\u67e5\u8a62\u6027\u80fd\u65b9\u9762\uff0cMySQL\u7684\u8907\u96dc\u67e5\u8a62\uff08\u5982JOIN\uff09\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\u53ef\u80fd\u6703\u8b8a\u5f97\u7de9\u6162\uff0c\u800cMongoDB\u5247\u80fd\u5920\u901a\u904e\u6587\u6a94\u67e5\u8a62\u5feb\u901f\u6aa2\u7d22\u6578\u64da\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u662fMySQL\u4e2d\u67e5\u8a62\u7528\u6236\u7684SQL\u8a9e\u53e5\uff1a<\/p>\n<pre><code>SELECT * FROM Users WHERE email = 'john@example.com';<\/code><\/pre>\n<p>\u800c\u5728MongoDB\u4e2d\uff0c\u67e5\u8a62\u7528\u6236\u7684\u64cd\u4f5c\u5247\u53ef\u4ee5\u9019\u6a23\u9032\u884c\uff1a<\/p>\n<pre><code>db.users.find({ email: 'john@example.com' });<\/code><\/pre>\n<h3 id=\"%e4%ba%8b%e5%8b%99%e6%94%af%e6%8c%81-DthnMvQOfD\">\u4e8b\u52d9\u652f\u6301<\/h3>\n<p>MySQL\u7684ACID\u7279\u6027\u4f7f\u5176\u5728\u9700\u8981\u4e8b\u52d9\u652f\u6301\u7684\u61c9\u7528\u4e2d\u8868\u73fe\u51fa\u8272\uff0c\u4f8b\u5982\u91d1\u878d\u7cfb\u7d71\u3002\u800cMongoDB\u5728\u65e9\u671f\u7248\u672c\u4e2d\u5c0d\u4e8b\u52d9\u7684\u652f\u6301\u6709\u9650\uff0c\u4f46\u96a8\u8457\u7248\u672c\u7684\u66f4\u65b0\uff0c\u73fe\u5728\u4e5f\u958b\u59cb\u652f\u6301\u591a\u6587\u6a94\u4e8b\u52d9\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-DthnMvQOfD\">\u7e3d\u7d50<\/h2>\n<p>\u7e3d\u7684\u4f86\u8aaa\uff0cMySQL\u548cMongoDB\u5404\u6709\u512a\u7f3a\u9ede\uff0c\u9078\u64c7\u54ea\u4e00\u7a2e\u6578\u64da\u5eab\u53d6\u6c7a\u65bc\u5177\u9ad4\u7684\u61c9\u7528\u9700\u6c42\u3002\u5982\u679c\u60a8\u7684\u61c9\u7528\u9700\u8981\u8907\u96dc\u7684\u67e5\u8a62\u548c\u4e8b\u52d9\u652f\u6301\uff0cMySQL\u53ef\u80fd\u662f\u66f4\u597d\u7684\u9078\u64c7\uff1b\u800c\u5982\u679c\u60a8\u9700\u8981\u9748\u6d3b\u7684\u6578\u64da\u6a21\u578b\u548c\u9ad8\u53ef\u64f4\u5c55\u6027\uff0cMongoDB\u5247\u66f4\u70ba\u5408\u9069\u3002\u7121\u8ad6\u9078\u64c7\u54ea\u7a2e\u6578\u64da\u5eab\uff0c\u78ba\u4fdd\u60a8\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u74b0\u5883\u80fd\u5920\u652f\u6301\u6240\u9700\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u90fd\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6bd4\u8f03MySQL\u8207MongoDB\u7684\u8a2d\u8a08\u5be6\u4f8b\uff0c\u63a2\u8a0e\u5169\u8005\u5728\u6578\u64da\u7d50\u69cb\u3001\u67e5\u8a62\u6027\u80fd\u53ca\u64f4\u5c55\u6027\u4e0a\u7684\u5dee\u7570\u8207\u512a\u52e2\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-180318","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180318","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=180318"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180318\/revisions"}],"predecessor-version":[{"id":180319,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180318\/revisions\/180319"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=180318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=180318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=180318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}