{"id":21968,"date":"2024-01-02T18:19:24","date_gmt":"2024-01-02T18:19:24","guid":{"rendered":"https:\/\/server.hk\/blog\/21968\/"},"modified":"2024-11-14T10:24:07","modified_gmt":"2024-11-14T02:24:07","slug":"mongodb-server-a-comprehensive-guide-for-beginners","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21968\/","title":{"rendered":"MongoDB \u4f3a\u670d\u5668\uff1a\u521d\u5b78\u8005\u5168\u9762\u6307\u5357"},"content":{"rendered":"<h1 id=\"mongodb-server-a-comprehensive-guide-for-beginners-gkKOXfhSUu\">MongoDB \u4f3a\u670d\u5668\uff1a\u521d\u5b78\u8005\u5168\u9762\u6307\u5357<\/h1>\n<p>MongoDB \u662f\u4e00\u7a2e\u6d41\u884c\u7684\u958b\u6e90 NoSQL \u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u63d0\u4f9b\u9ad8\u6027\u80fd\u3001\u53ef\u64f4\u5c55\u6027\u548c\u9748\u6d3b\u6027\uff0c\u9069\u5408\u73fe\u4ee3\u61c9\u7528\u3002\u5728\u9019\u4efd\u5168\u9762\u7684\u6307\u5357\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e MongoDB \u4f3a\u670d\u5668\u7684\u57fa\u790e\u77e5\u8b58\u3001\u5176\u95dc\u9375\u7279\u9ede\uff0c\u4ee5\u53ca\u5982\u4f55\u958b\u59cb\u4f7f\u7528\u3002<\/p>\n<h2 id=\"what-is-mongodb-gkKOXfhSUu\">\u4ec0\u9ebc\u662f MongoDB\uff1f<\/h2>\n<p>MongoDB \u662f\u4e00\u7a2e\u4ee5\u6587\u4ef6\u70ba\u5c0e\u5411\u7684\u6578\u64da\u5eab\uff0c\u4ee5\u9748\u6d3b\u7684\u3001\u985e\u4f3c JSON \u7684\u6587\u6a94\u5f62\u5f0f\u5b58\u5132\u6578\u64da\uff0c\u7a31\u70ba BSON\uff08Binary JSON\uff09\u3002\u8207\u50b3\u7d71\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u4e0d\u540c\uff0cMongoDB \u4e0d\u4f7f\u7528\u8868\u683c\u3001\u884c\u548c\u5217\u3002\u76f8\u53cd\uff0c\u5b83\u4f7f\u7528\u96c6\u5408\u548c\u6587\u6a94\u4f86\u7d44\u7e54\u548c\u8868\u793a\u6578\u64da\u3002<\/p>\n<p>MongoDB \u7684\u4e00\u500b\u95dc\u9375\u512a\u52e2\u662f\u5b83\u80fd\u5920\u8655\u7406\u975e\u7d50\u69cb\u5316\u548c\u534a\u7d50\u69cb\u5316\u6578\u64da\u3002\u9019\u4f7f\u5176\u6210\u70ba\u8a2d\u8a08\u9700\u8981\u8655\u7406\u5feb\u901f\u8b8a\u5316\u6578\u64da\u6216\u9700\u8981\u9748\u6d3b\u6a21\u5f0f\u7684\u61c9\u7528\u7a0b\u5f0f\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"key-features-of-mongodb-gkKOXfhSUu\">MongoDB \u7684\u95dc\u9375\u7279\u9ede<\/h2>\n<p>1. <strong>\u53ef\u64f4\u5c55\u6027\uff1a<\/strong> MongoDB \u8a2d\u8a08\u70ba\u901a\u904e\u5728\u591a\u53f0\u4f3a\u670d\u5668\u4e4b\u9593\u5206\u4f48\u6578\u64da\u4f86\u6c34\u5e73\u64f4\u5c55\u3002\u5b83\u652f\u6301\u81ea\u52d5\u5206\u584a\uff0c\u5141\u8a31\u60a8\u5206\u5272\u6578\u64da\u4e26\u5c07\u5176\u5206\u4f48\u5728\u4e00\u500b\u6a5f\u5668\u96c6\u7fa4\u4e2d\u3002<\/p>\n<p>2. <strong>\u9ad8\u6027\u80fd\uff1a<\/strong> MongoDB \u901a\u904e\u4f7f\u7528\u5167\u5b58\u7de9\u5b58\u548c\u7d22\u5f15\u4f86\u63d0\u4f9b\u9ad8\u6548\u7684\u8b80\u5beb\u64cd\u4f5c\u3002\u5b83\u9084\u652f\u6301\u5404\u7a2e\u67e5\u8a62\u512a\u5316\u6280\u8853\u4f86\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\u3002<\/p>\n<p>3. <strong>\u9748\u6d3b\u7684\u6578\u64da\u6a21\u578b\uff1a<\/strong> MongoDB \u7684\u9748\u6d3b\u6578\u64da\u6a21\u578b\u4f7f\u60a8\u80fd\u5920\u8f15\u9b06\u5b58\u5132\u548c\u6aa2\u7d22\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\u3002\u5b83\u652f\u6301\u5d4c\u5957\u6587\u6a94\u3001\u6578\u7d44\u548c\u9375\u503c\u5c0d\uff0c\u9069\u5408\u8655\u7406\u591a\u7a2e\u6578\u64da\u985e\u578b\u3002<\/p>\n<p>4. <strong>\u8907\u88fd\u548c\u5bb9\u932f\u80fd\u529b\uff1a<\/strong> MongoDB \u652f\u6301\u8907\u672c\u96c6\uff0c\u5b83\u5011\u662f\u63d0\u4f9b\u9ad8\u53ef\u7528\u6027\u548c\u5bb9\u932f\u529f\u80fd\u7684\u81ea\u6211\u4fee\u5fa9\u96c6\u7fa4\u3002\u8907\u672c\u96c6\u5982\u679c\u7576\u524d\u4e3b\u7bc0\u9ede\u5931\u6548\uff0c\u6703\u81ea\u52d5\u9078\u8209\u65b0\u7684\u4e3b\u7bc0\u9ede\u3002<\/p>\n<p>5. <strong>\u8c50\u5bcc\u7684\u67e5\u8a62\u8a9e\u8a00\uff1a<\/strong> MongoDB \u63d0\u4f9b\u4e86\u4e00\u7a2e\u529f\u80fd\u5f37\u5927\u7684\u67e5\u8a62\u8a9e\u8a00\uff0c\u652f\u6301\u5ee3\u6cdb\u7684\u67e5\u8a62\uff0c\u5305\u62ec\u81e8\u6642\u67e5\u8a62\u3001\u7bc4\u570d\u67e5\u8a62\u548c\u5730\u7406\u7a7a\u9593\u67e5\u8a62\u3002\u5b83\u9084\u652f\u6301\u805a\u5408\u7ba1\u9053\uff0c\u7528\u65bc\u8907\u96dc\u7684\u6578\u64da\u8655\u7406\u3002<\/p>\n<h2 id=\"getting-started-with-mongodb-gkKOXfhSUu\">\u5982\u4f55\u958b\u59cb\u4f7f\u7528 MongoDB<\/h2>\n<p>\u8981\u958b\u59cb\u4f7f\u7528 MongoDB\uff0c\u8acb\u6309\u7167\u4ee5\u4e0b\u6b65\u9a5f\u64cd\u4f5c\uff1a<\/p>\n<h3 id=\"step-1-install-mongodb-gkKOXfhSUu\">\u6b65\u9a5f 1: \u5b89\u88dd MongoDB<\/h3>\n<p>\u5f9e\u5b98\u65b9\u7db2\u7ad9\uff08<a href=\"https:\/\/www.mongodb.com\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/www.mongodb.com<\/a>\uff09\u4e0b\u8f09\u4e26\u5b89\u88dd MongoDB\u3002MongoDB \u70ba\u5305\u62ec Windows\u3001macOS \u548c Linux \u5728\u5167\u7684\u591a\u7a2e\u64cd\u4f5c\u7cfb\u7d71\u63d0\u4f9b\u5b89\u88dd\u7a0b\u5f0f\u3002<\/p>\n<h3 id=\"step-2-start-mongodb-server-gkKOXfhSUu\">\u6b65\u9a5f 2: \u555f\u52d5 MongoDB \u4f3a\u670d\u5668<\/h3>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u6839\u64da\u60a8\u7684\u64cd\u4f5c\u7cfb\u7d71\u904b\u884c\u9069\u7576\u7684\u547d\u4ee4\u4f86\u555f\u52d5 MongoDB \u4f3a\u670d\u5668\u3002\u9810\u8a2d\u60c5\u6cc1\u4e0b\uff0cMongoDB \u76e3\u807d 27017 \u7aef\u53e3\u3002<\/p>\n<pre><code>$ mongod\r\n<\/code><\/pre>\n<h3 id=\"step-3-connect-to-mongodb-gkKOXfhSUu\">\u6b65\u9a5f 3: \u9023\u63a5\u5230 MongoDB<\/h3>\n<p>\u958b\u555f\u65b0\u7684\u7d42\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u4f7f\u7528 MongoDB shell \u9023\u63a5\u5230 MongoDB \u4f3a\u670d\u5668\u3002<\/p>\n<pre><code>$ mongo\r\n<\/code><\/pre>\n<h3 id=\"step-4-create-a-database-gkKOXfhSUu\">\u6b65\u9a5f 4: \u5275\u5efa\u6578\u64da\u5eab<\/h3>\n<p>\u9023\u63a5\u6210\u529f\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5275\u5efa\u65b0\u7684\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>use mydatabase\r\n<\/code><\/pre>\n<h3 id=\"step-5-create-a-collection-gkKOXfhSUu\">\u6b65\u9a5f 5: \u5275\u5efa\u96c6\u5408<\/h3>\n<p>\u5728\u6578\u64da\u5eab\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5275\u5efa\u4e00\u500b\u96c6\u5408\uff1a<\/p>\n<pre><code>db.createCollection(\"mycollection\")\r\n<\/code><\/pre>\n<h3 id=\"step-6-insert-documents-gkKOXfhSUu\">\u6b65\u9a5f 6: \u63d2\u5165\u6587\u6a94<\/h3>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c07\u6587\u6a94\u63d2\u5165\u96c6\u5408\u4e2d\uff1a<\/p>\n<pre><code>db.mycollection.insertOne({ name: \"John Doe\", age: 30 })\r\n<\/code><\/pre>\n<h3 id=\"step-7-query-documents-gkKOXfhSUu\">\u6b65\u9a5f 7: \u67e5\u8a62\u6587\u6a94<\/h3>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5f9e\u96c6\u5408\u4e2d\u67e5\u8a62\u6587\u6a94\uff1a<\/p>\n<pre><code>db.mycollection.find()\r\n<\/code><\/pre>\n<h2 id=\"summary-gkKOXfhSUu\">\u7e3d\u7d50<\/h2>\n<p>MongoDB \u662f\u4e00\u7a2e\u529f\u80fd\u5f37\u5927\u4e14\u9748\u6d3b\u7684 NoSQL \u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u53ef\u64f4\u5c55\u6027\u3001\u9ad8\u6027\u80fd\u548c\u8c50\u5bcc\u7684\u529f\u80fd\u96c6\u3002\u5728\u9019\u4efd\u6307\u5357\u4e2d\uff0c\u6211\u5011\u63a2\u7d22\u4e86 MongoDB \u4f3a\u670d\u5668\u7684\u57fa\u790e\u77e5\u8b58\u3001\u5176\u95dc\u9375\u7279\u9ede\uff0c\u4ee5\u53ca\u5982\u4f55\u958b\u59cb\u4f7f\u7528\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u4e00\u500b\u53ef\u9760\u4e14\u9ad8\u6548\u7684 VPS \u6258\u7ba1\u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684 MongoDB \u670d\u52d9\u5668\uff0c\u8acb\u8003\u616e<a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002\u901a\u904e\u4ed6\u5011\u7684\u9802\u7d1a VPS \u89e3\u6c7a\u65b9\u6848\uff0c\u60a8\u53ef\u4ee5\u78ba\u4fdd MongoDB \u61c9\u7528\u7a0b\u5e8f\u7684\u6700\u4f73\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB \u4f3a\u670d\u5668\uff1a\u521d\u5b78\u8005\u5168&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-21968","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21968","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=21968"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21968\/revisions"}],"predecessor-version":[{"id":23342,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21968\/revisions\/23342"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}