{"id":159965,"date":"2024-11-03T19:09:20","date_gmt":"2024-11-03T11:09:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/159965\/"},"modified":"2024-11-03T19:09:21","modified_gmt":"2024-11-03T11:09:21","slug":"mongodb-%e6%95%99%e7%a8%8b%ef%bc%9amongodb-%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/159965\/","title":{"rendered":"MongoDB \u6559\u7a0b\uff1aMongoDB \u7d22\u5f15"},"content":{"rendered":"<h1 id=\"mongodb-%e6%95%99%e7%a8%8b%ef%bc%9amongodb-%e7%b4%a2%e5%bc%95-uepOiokcGi\">MongoDB \u6559\u7a0b\uff1aMongoDB \u7d22\u5f15<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u6027\u80fd\u548c\u6548\u7387\u81f3\u95dc\u91cd\u8981\u3002MongoDB \u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684 NoSQL \u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u9748\u6d3b\u7684\u6578\u64da\u6a21\u578b\u548c\u9ad8\u6548\u7684\u67e5\u8a62\u80fd\u529b\u3002\u7d22\u5f15\u662f\u63d0\u5347 MongoDB \u67e5\u8a62\u6027\u80fd\u7684\u95dc\u9375\u5de5\u5177\u4e4b\u4e00\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e MongoDB \u7684\u7d22\u5f15\uff0c\u5305\u62ec\u5176\u985e\u578b\u3001\u4f7f\u7528\u65b9\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e7%b4%a2%e5%bc%95%ef%bc%9f-uepOiokcGi\">\u4ec0\u9ebc\u662f\u7d22\u5f15\uff1f<\/h2>\n<p>\u7d22\u5f15\u662f\u4e00\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u7528\u65bc\u52a0\u901f\u6578\u64da\u6aa2\u7d22\u64cd\u4f5c\u3002\u5b83\u985e\u4f3c\u65bc\u66f8\u7c4d\u7684\u76ee\u9304\uff0c\u5e6b\u52a9\u6578\u64da\u5eab\u5feb\u901f\u5b9a\u4f4d\u6240\u9700\u7684\u6578\u64da\u3002\u6c92\u6709\u7d22\u5f15\uff0cMongoDB \u9700\u8981\u6383\u63cf\u6574\u500b\u96c6\u5408\u4f86\u67e5\u627e\u6578\u64da\uff0c\u9019\u5728\u6578\u64da\u91cf\u9f90\u5927\u6642\u6703\u5c0e\u81f4\u6027\u80fd\u4e0b\u964d\u3002<\/p>\n<h2 id=\"mongodb-%e7%9a%84%e7%b4%a2%e5%bc%95%e9%a1%9e%e5%9e%8b-uepOiokcGi\">MongoDB \u7684\u7d22\u5f15\u985e\u578b<\/h2>\n<p>MongoDB \u63d0\u4f9b\u4e86\u591a\u7a2e\u7d22\u5f15\u985e\u578b\uff0c\u4ee5\u6eff\u8db3\u4e0d\u540c\u7684\u67e5\u8a62\u9700\u6c42\uff1a<\/p>\n<ul>\n<li><strong>\u55ae\u5b57\u6bb5\u7d22\u5f15<\/strong>\uff1a\u6700\u57fa\u672c\u7684\u7d22\u5f15\u985e\u578b\uff0c\u91dd\u5c0d\u96c6\u5408\u4e2d\u7684\u55ae\u500b\u5b57\u6bb5\u9032\u884c\u7d22\u5f15\u3002<\/li>\n<li><strong>\u8907\u5408\u7d22\u5f15<\/strong>\uff1a\u91dd\u5c0d\u591a\u500b\u5b57\u6bb5\u9032\u884c\u7d22\u5f15\uff0c\u9069\u5408\u9700\u8981\u540c\u6642\u67e5\u8a62\u591a\u500b\u5b57\u6bb5\u7684\u60c5\u6cc1\u3002<\/li>\n<li><strong>\u552f\u4e00\u7d22\u5f15<\/strong>\uff1a\u78ba\u4fdd\u7d22\u5f15\u5b57\u6bb5\u7684\u503c\u552f\u4e00\uff0c\u9632\u6b62\u91cd\u8907\u6578\u64da\u7684\u63d2\u5165\u3002<\/li>\n<li><strong>\u5168\u6587\u7d22\u5f15<\/strong>\uff1a\u7528\u65bc\u5c0d\u6587\u672c\u9032\u884c\u641c\u7d22\uff0c\u652f\u6301\u95dc\u9375\u5b57\u67e5\u8a62\u3002<\/li>\n<li><strong>\u5730\u7406\u7a7a\u9593\u7d22\u5f15<\/strong>\uff1a\u5c08\u9580\u7528\u65bc\u5730\u7406\u4f4d\u7f6e\u6578\u64da\u7684\u67e5\u8a62\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%89%b5%e5%bb%ba%e7%b4%a2%e5%bc%95-uepOiokcGi\">\u5982\u4f55\u5275\u5efa\u7d22\u5f15<\/h2>\n<p>\u5728 MongoDB \u4e2d\uff0c\u5275\u5efa\u7d22\u5f15\u975e\u5e38\u7c21\u55ae\u3002\u53ef\u4ee5\u4f7f\u7528 <code>createIndex()<\/code> \u65b9\u6cd5\u4f86\u5275\u5efa\u7d22\u5f15\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u55ae\u5b57\u6bb5\u7d22\u5f15\u548c\u8907\u5408\u7d22\u5f15\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\n\/\/ \u5275\u5efa\u55ae\u5b57\u6bb5\u7d22\u5f15\ndb.collection.createIndex({ fieldName: 1 })\n\n\/\/ \u5275\u5efa\u8907\u5408\u7d22\u5f15\ndb.collection.createIndex({ field1: 1, field2: -1 })\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u78bc\u4e2d\uff0c<code>1<\/code> \u8868\u793a\u5347\u5e8f\u7d22\u5f15\uff0c\u800c <code>-1<\/code> \u8868\u793a\u964d\u5e8f\u7d22\u5f15\u3002\u6839\u64da\u67e5\u8a62\u7684\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6392\u5e8f\u65b9\u5f0f\u3002<\/p>\n<h2 id=\"%e7%b4%a2%e5%bc%95%e7%9a%84%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-uepOiokcGi\">\u7d22\u5f15\u7684\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u96d6\u7136\u7d22\u5f15\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\uff0c\u4f46\u4e0d\u7576\u4f7f\u7528\u4e5f\u53ef\u80fd\u5c0e\u81f4\u6027\u80fd\u4e0b\u964d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5be6\u8e10\uff1a<\/p>\n<ul>\n<li><strong>\u907f\u514d\u904e\u591a\u7d22\u5f15<\/strong>\uff1a\u6bcf\u500b\u7d22\u5f15\u90fd\u6703\u4f54\u7528\u984d\u5916\u7684\u5b58\u5132\u7a7a\u9593\uff0c\u4e26\u5728\u6578\u64da\u63d2\u5165\u3001\u66f4\u65b0\u6642\u589e\u52a0\u958b\u92b7\u3002<\/li>\n<li><strong>\u5b9a\u671f\u6aa2\u67e5\u7d22\u5f15\u4f7f\u7528\u60c5\u6cc1<\/strong>\uff1a\u4f7f\u7528 <code>db.collection.getIndexes()<\/code> \u67e5\u770b\u7576\u524d\u7d22\u5f15\uff0c\u4e26\u6839\u64da\u67e5\u8a62\u9700\u6c42\u9032\u884c\u8abf\u6574\u3002<\/li>\n<li><strong>\u4f7f\u7528\u8986\u84cb\u7d22\u5f15<\/strong>\uff1a\u5982\u679c\u67e5\u8a62\u53ea\u6d89\u53ca\u7d22\u5f15\u4e2d\u7684\u5b57\u6bb5\uff0cMongoDB \u53ef\u4ee5\u76f4\u63a5\u5f9e\u7d22\u5f15\u4e2d\u8fd4\u56de\u7d50\u679c\uff0c\u9019\u6a23\u53ef\u4ee5\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-uepOiokcGi\">\u7d50\u8ad6<\/h2>\n<p>MongoDB \u7684\u7d22\u5f15\u662f\u63d0\u5347\u67e5\u8a62\u6027\u80fd\u7684\u91cd\u8981\u5de5\u5177\u3002\u901a\u904e\u5408\u7406\u5730\u4f7f\u7528\u4e0d\u540c\u985e\u578b\u7684\u7d22\u5f15\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u97ff\u61c9\u901f\u5ea6\u548c\u6548\u7387\u3002\u5728\u8a2d\u8a08\u6578\u64da\u6a21\u578b\u6642\uff0c\u61c9\u5145\u5206\u8003\u616e\u7d22\u5f15\u7684\u4f7f\u7528\uff0c\u4e26\u5b9a\u671f\u6aa2\u67e5\u548c\u512a\u5316\u7d22\u5f15\u914d\u7f6e\uff0c\u4ee5\u78ba\u4fdd\u6700\u4f73\u6027\u80fd\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc <a href=\"https:\/\/server.hk\">VPS<\/a> \u548c\u6578\u64da\u5eab\u7ba1\u7406\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b66\u4e60 MongoDB \u7d22\u5f15\u7684\u57fa\u672c\u6982\u5ff5\u548c\u4f7f\u7528\u6280\u5de7\uff0c\u63d0\u5347\u6570\u636e\u5e93\u67e5\u8be2\u6027\u80fd\uff0c\u4f18\u5316\u6570\u636e\u68c0\u7d22\u6548\u7387\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-159965","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/159965","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=159965"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/159965\/revisions"}],"predecessor-version":[{"id":159966,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/159965\/revisions\/159966"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=159965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=159965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=159965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}