{"id":167033,"date":"2024-11-05T21:48:26","date_gmt":"2024-11-05T13:48:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/167033\/"},"modified":"2024-11-05T21:48:27","modified_gmt":"2024-11-05T13:48:27","slug":"%e5%88%a9%e7%94%a8mongodb%e5%af%a6%e7%8f%be%e8%87%aa%e5%ae%9a%e7%be%a9%e5%87%bd%e6%95%b8%e7%9a%84%e6%93%8d%e4%bd%9c%ef%bc%88mongodb%e8%87%aa%e5%ae%9a%e7%be%a9%e5%87%bd%e6%95%b8%ef%bc%89-2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/167033\/","title":{"rendered":"\u5229\u7528mongodb\u5be6\u73fe\u81ea\u5b9a\u7fa9\u51fd\u6578\u7684\u64cd\u4f5c\uff08mongodb\u81ea\u5b9a\u7fa9\u51fd\u6578\uff09"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8mongodb%e5%af%a6%e7%8f%be%e8%87%aa%e5%ae%9a%e7%be%a9%e5%87%bd%e6%95%b8%e7%9a%84%e6%93%8d%e4%bd%9c%ef%bc%88mongodb%e8%87%aa%e5%ae%9a%e7%be%a9%e5%87%bd%e6%95%b8%ef%bc%89-PqQUOGSESn\">\u5229\u7528MongoDB\u5be6\u73fe\u81ea\u5b9a\u7fa9\u51fd\u6578\u7684\u64cd\u4f5c\uff08MongoDB\u81ea\u5b9a\u7fa9\u51fd\u6578\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0cMongoDB\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684NoSQL\u6578\u64da\u5eab\uff0c\u56e0\u5176\u9748\u6d3b\u6027\u548c\u53ef\u64f4\u5c55\u6027\u800c\u53d7\u5230\u5ee3\u6cdb\u4f7f\u7528\u3002MongoDB\u4e0d\u50c5\u652f\u6301\u57fa\u672c\u7684CRUD\u64cd\u4f5c\uff0c\u9084\u5141\u8a31\u7528\u6236\u5275\u5efa\u81ea\u5b9a\u7fa9\u51fd\u6578\uff0c\u4ee5\u6eff\u8db3\u7279\u5b9a\u7684\u696d\u52d9\u9700\u6c42\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728MongoDB\u4e2d\u5be6\u73fe\u81ea\u5b9a\u7fa9\u51fd\u6578\u7684\u64cd\u4f5c\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afmongodb%e8%87%aa%e5%ae%9a%e7%be%a9%e5%87%bd%e6%95%b8%ef%bc%9f-PqQUOGSESn\">\u4ec0\u9ebc\u662fMongoDB\u81ea\u5b9a\u7fa9\u51fd\u6578\uff1f<\/h2>\n<p>\u81ea\u5b9a\u7fa9\u51fd\u6578\u662f\u7528\u6236\u5b9a\u7fa9\u7684\u51fd\u6578\uff0c\u5141\u8a31\u958b\u767c\u8005\u5728MongoDB\u4e2d\u57f7\u884c\u7279\u5b9a\u7684\u908f\u8f2f\u6216\u8a08\u7b97\u3002\u9019\u4e9b\u51fd\u6578\u53ef\u4ee5\u5728\u67e5\u8a62\u4e2d\u4f7f\u7528\uff0c\u4e26\u4e14\u53ef\u4ee5\u63d0\u9ad8\u6578\u64da\u8655\u7406\u7684\u6548\u7387\u3002MongoDB\u652f\u6301JavaScript\u4f5c\u70ba\u81ea\u5b9a\u7fa9\u51fd\u6578\u7684\u7de8\u5beb\u8a9e\u8a00\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u5229\u7528\u5176\u5f37\u5927\u7684\u8a9e\u6cd5\u548c\u529f\u80fd\u3002<\/p>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%89%b5%e5%bb%ba%e8%87%aa%e5%ae%9a%e7%be%a9%e5%87%bd%e6%95%b8-PqQUOGSESn\">\u5982\u4f55\u5275\u5efa\u81ea\u5b9a\u7fa9\u51fd\u6578<\/h2>\n<p>\u5728MongoDB\u4e2d\u5275\u5efa\u81ea\u5b9a\u7fa9\u51fd\u6578\u7684\u904e\u7a0b\u76f8\u5c0d\u7c21\u55ae\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u7fa9\u51fd\u6578\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e9%80%a3%e6%8e%a5%e5%88%b0mongodb-PqQUOGSESn\">1. \u9023\u63a5\u5230MongoDB<\/h3>\n<pre><code>const { MongoClient } = require('mongodb');\nconst uri = \"mongodb:\/\/localhost:27017\";\nconst client = new MongoClient(uri);\n\nasync function run() {\n    try {\n        await client.connect();\n        console.log(\"\u6210\u529f\u9023\u63a5\u5230MongoDB\");\n    } finally {\n        await client.close();\n    }\n}\nrun().catch(console.dir);<\/code><\/pre>\n<h3 id=\"2-%e5%ae%9a%e7%be%a9%e8%87%aa%e5%ae%9a%e7%be%a9%e5%87%bd%e6%95%b8-PqQUOGSESn\">2. \u5b9a\u7fa9\u81ea\u5b9a\u7fa9\u51fd\u6578<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528MongoDB\u7684<code>db.runCommand()<\/code>\u65b9\u6cd5\u4f86\u5b9a\u7fa9\u81ea\u5b9a\u7fa9\u51fd\u6578\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u8a72\u51fd\u6578\u5c07\u5169\u500b\u6578\u5b57\u76f8\u52a0\uff1a<\/p>\n<pre><code>db.runCommand({\n    eval: function(a, b) {\n        return a + b;\n    }\n});<\/code><\/pre>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8%e8%87%aa%e5%ae%9a%e7%be%a9%e5%87%bd%e6%95%b8-PqQUOGSESn\">3. \u4f7f\u7528\u81ea\u5b9a\u7fa9\u51fd\u6578<\/h3>\n<p>\u5b9a\u7fa9\u5b8c\u81ea\u5b9a\u7fa9\u51fd\u6578\u5f8c\uff0c\u53ef\u4ee5\u5728\u67e5\u8a62\u4e2d\u8abf\u7528\u5b83\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u525b\u624d\u5b9a\u7fa9\u7684\u52a0\u6cd5\u51fd\u6578\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>const result = db.eval(\"function(a, b) { return a + b; }\", 5, 10);\nconsole.log(result); \/\/ \u8f38\u51fa\uff1a15<\/code><\/pre>\n<h2 id=\"%e8%87%aa%e5%ae%9a%e7%be%a9%e5%87%bd%e6%95%b8%e7%9a%84%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-PqQUOGSESn\">\u81ea\u5b9a\u7fa9\u51fd\u6578\u7684\u61c9\u7528\u5834\u666f<\/h2>\n<p>\u81ea\u5b9a\u7fa9\u51fd\u6578\u5728\u591a\u7a2e\u5834\u666f\u4e2d\u90fd\u80fd\u767c\u63ee\u4f5c\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u64da\u8655\u7406\uff1a<\/strong>\u53ef\u4ee5\u7528\u4f86\u9032\u884c\u8907\u96dc\u7684\u6578\u64da\u8a08\u7b97\u548c\u8f49\u63db\u3002<\/li>\n<li><strong>\u6578\u64da\u9a57\u8b49\uff1a<\/strong>\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u6578\u64da\u7684\u6709\u6548\u6027\u548c\u5b8c\u6574\u6027\u3002<\/li>\n<li><strong>\u696d\u52d9\u908f\u8f2f\uff1a<\/strong>\u53ef\u4ee5\u5c07\u7279\u5b9a\u7684\u696d\u52d9\u908f\u8f2f\u5c01\u88dd\u5728\u51fd\u6578\u4e2d\uff0c\u4fbf\u65bc\u91cd\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-PqQUOGSESn\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528\u81ea\u5b9a\u7fa9\u51fd\u6578\u6642\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u6027\u80fd\uff1a\u81ea\u5b9a\u7fa9\u51fd\u6578\u53ef\u80fd\u6703\u5f71\u97ff\u67e5\u8a62\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\u3002<\/li>\n<li>\u5b89\u5168\u6027\uff1a\u4f7f\u7528<code>eval<\/code>\u6642\u9700\u8b39\u614e\uff0c\u56e0\u70ba\u5b83\u53ef\u80fd\u6703\u57f7\u884c\u4e0d\u5b89\u5168\u7684\u4ee3\u78bc\u3002<\/li>\n<li>\u53ef\u7dad\u8b77\u6027\uff1a\u904e\u591a\u7684\u81ea\u5b9a\u7fa9\u51fd\u6578\u53ef\u80fd\u6703\u4f7f\u4ee3\u78bc\u8b8a\u5f97\u96e3\u4ee5\u7dad\u8b77\uff0c\u61c9\u9069\u5ea6\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-PqQUOGSESn\">\u7e3d\u7d50<\/h2>\n<p>MongoDB\u7684\u81ea\u5b9a\u7fa9\u51fd\u6578\u529f\u80fd\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u9748\u6d3b\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\uff0c\u80fd\u5920\u6eff\u8db3\u7279\u5b9a\u7684\u696d\u52d9\u9700\u6c42\u3002\u901a\u904e\u7c21\u55ae\u7684\u6b65\u9a5f\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5275\u5efa\u548c\u4f7f\u7528\u81ea\u5b9a\u7fa9\u51fd\u6578\uff0c\u5f9e\u800c\u63d0\u9ad8\u6578\u64da\u64cd\u4f5c\u7684\u6548\u7387\u3002\u5728\u9078\u64c7\u5408\u9069\u7684<code>VPS<\/code>\u89e3\u6c7a\u65b9\u6848\u6642\uff0c\u8003\u616e\u5230\u6027\u80fd\u548c\u5b89\u5168\u6027\uff0c<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u63d0\u4f9b\u4e86\u7a69\u5b9a\u7684\u74b0\u5883\u4f86\u904b\u884cMongoDB\uff0c\u5e6b\u52a9\u4f01\u696d\u66f4\u597d\u5730\u7ba1\u7406\u548c\u8655\u7406\u6578\u64da\u3002\u7121\u8ad6\u662f\u9078\u64c7<code>\u4e91\u4f3a\u670d\u5668<\/code>\u9084\u662f<code>\u9999\u6e2f\u4f3a\u670d\u5668<\/code>\uff0c\u90fd\u80fd\u70ba\u60a8\u7684\u696d\u52d9\u63d0\u4f9b\u5f37\u6709\u529b\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5728MongoDB\u4e2d\u5be6\u73fe\u81ea\u5b9a\u7fa9\u51fd\u6578\uff0c\u63d0\u5347\u6578\u64da\u64cd\u4f5c\u6548\u7387\uff0c\u6eff\u8db3\u7279\u5b9a\u696d\u52d9\u9700\u6c42\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-167033","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167033","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=167033"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167033\/revisions"}],"predecessor-version":[{"id":167034,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167033\/revisions\/167034"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=167033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=167033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=167033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}