{"id":63712,"date":"2024-10-14T01:50:17","date_gmt":"2024-10-13T17:50:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/63712\/"},"modified":"2024-10-14T01:50:17","modified_gmt":"2024-10-13T17:50:17","slug":"mongodb%e8%a1%93%e8%aa%9e-%e5%a4%9a%e9%8d%b5%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/63712\/","title":{"rendered":"MongoDB\u8853\u8a9e &#8211; \u591a\u9375\u7d22\u5f15"},"content":{"rendered":"<h1 id=\"mongodb%e8%a1%93%e8%aa%9e-%e5%a4%9a%e9%8d%b5%e7%b4%a2%e5%bc%95-CNCDbuZqAE\">MongoDB\u8853\u8a9e &#8211; \u591a\u9375\u7d22\u5f15<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u9078\u64c7\u548c\u8a2d\u8a08\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u81f3\u95dc\u91cd\u8981\u3002MongoDB\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684NoSQL\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u7d22\u5f15\u6a5f\u5236\u4f86\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\u3002\u5176\u4e2d\uff0c\u591a\u9375\u7d22\u5f15\uff08Multikey Index\uff09\u662f\u4e00\u500b\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5305\u542b\u6578\u7d44\u7684\u6587\u6a94\u6642\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u591a\u9375\u7d22\u5f15\u7684\u5b9a\u7fa9\u3001\u7279\u6027\u53ca\u5176\u4f7f\u7528\u5834\u666f\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e5%a4%9a%e9%8d%b5%e7%b4%a2%e5%bc%95%ef%bc%9f-CNCDbuZqAE\">\u4ec0\u9ebc\u662f\u591a\u9375\u7d22\u5f15\uff1f<\/h2>\n<p>\u591a\u9375\u7d22\u5f15\u662fMongoDB\u4e2d\u4e00\u7a2e\u7279\u6b8a\u7684\u7d22\u5f15\u985e\u578b\uff0c\u5141\u8a31\u5c0d\u5305\u542b\u6578\u7d44\u7684\u5b57\u6bb5\u9032\u884c\u7d22\u5f15\u3002\u7576\u4e00\u500b\u6587\u6a94\u7684\u67d0\u500b\u5b57\u6bb5\u5305\u542b\u6578\u7d44\u6642\uff0cMongoDB\u6703\u81ea\u52d5\u70ba\u8a72\u5b57\u6bb5\u5275\u5efa\u591a\u9375\u7d22\u5f15\u3002\u9019\u6a23\uff0c\u6578\u7d44\u4e2d\u7684\u6bcf\u500b\u5143\u7d20\u90fd\u6703\u88ab\u7d22\u5f15\uff0c\u5f9e\u800c\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\u3002<\/p>\n<h2 id=\"%e5%a4%9a%e9%8d%b5%e7%b4%a2%e5%bc%95%e7%9a%84%e7%89%b9%e6%80%a7-CNCDbuZqAE\">\u591a\u9375\u7d22\u5f15\u7684\u7279\u6027<\/h2>\n<ul>\n<li><strong>\u81ea\u52d5\u5275\u5efa\uff1a<\/strong>\u7576\u4f60\u5728\u4e00\u500b\u5305\u542b\u6578\u7d44\u7684\u5b57\u6bb5\u4e0a\u5275\u5efa\u7d22\u5f15\u6642\uff0cMongoDB\u6703\u81ea\u52d5\u5c07\u5176\u8a2d\u7f6e\u70ba\u591a\u9375\u7d22\u5f15\u3002<\/li>\n<li><strong>\u67e5\u8a62\u6548\u7387\uff1a<\/strong>\u591a\u9375\u7d22\u5f15\u80fd\u5920\u52a0\u901f\u5c0d\u6578\u7d44\u5143\u7d20\u7684\u67e5\u8a62\uff0c\u7279\u5225\u662f\u5728\u4f7f\u7528$elemMatch\u3001$in\u7b49\u64cd\u4f5c\u7b26\u6642\u3002<\/li>\n<li><strong>\u9650\u5236\uff1a<\/strong>\u6bcf\u500b\u6587\u6a94\u53ea\u80fd\u6709\u4e00\u500b\u591a\u9375\u7d22\u5f15\uff0c\u4e14\u8a72\u7d22\u5f15\u4e0d\u80fd\u8207\u5176\u4ed6\u7d22\u5f15\u985e\u578b\u6df7\u5408\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a4%9a%e9%8d%b5%e7%b4%a2%e5%bc%95%e7%9a%84%e4%bd%bf%e7%94%a8%e5%a0%b4%e6%99%af-CNCDbuZqAE\">\u591a\u9375\u7d22\u5f15\u7684\u4f7f\u7528\u5834\u666f<\/h2>\n<p>\u591a\u9375\u7d22\u5f15\u7279\u5225\u9069\u5408\u4ee5\u4e0b\u5e7e\u7a2e\u60c5\u6cc1\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u7d44\u67e5\u8a62\uff1a<\/strong>\u7576\u4f60\u7684\u6587\u6a94\u4e2d\u6709\u6578\u7d44\u5b57\u6bb5\uff0c\u4e26\u4e14\u9700\u8981\u983b\u7e41\u67e5\u8a62\u6578\u7d44\u4e2d\u7684\u5143\u7d20\u6642\uff0c\u591a\u9375\u7d22\u5f15\u80fd\u986f\u8457\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\u3002<\/li>\n<li><strong>\u8907\u96dc\u67e5\u8a62\uff1a<\/strong>\u5728\u9700\u8981\u4f7f\u7528\u8907\u96dc\u67e5\u8a62\u689d\u4ef6\uff08\u5982$elemMatch\uff09\u6642\uff0c\u591a\u9375\u7d22\u5f15\u53ef\u4ee5\u5e6b\u52a9MongoDB\u66f4\u5feb\u5730\u5b9a\u4f4d\u5230\u7b26\u5408\u689d\u4ef6\u7684\u6587\u6a94\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%89%b5%e5%bb%ba%e5%a4%9a%e9%8d%b5%e7%b4%a2%e5%bc%95-CNCDbuZqAE\">\u5982\u4f55\u5275\u5efa\u591a\u9375\u7d22\u5f15<\/h2>\n<p>\u5728MongoDB\u4e2d\u5275\u5efa\u591a\u9375\u7d22\u5f15\u975e\u5e38\u7c21\u55ae\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u4e00\u500b\u5305\u542b\u6578\u7d44\u7684\u5b57\u6bb5\u4e0a\u5275\u5efa\u591a\u9375\u7d22\u5f15\uff1a<\/p>\n<pre><code>db.collection.createIndex({ \"tags\": 1 })<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba\u201ccollection\u201d\u7684\u96c6\u5408\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u500b\u540d\u70ba\u201ctags\u201d\u7684\u6578\u7d44\u5b57\u6bb5\u3002\u57f7\u884c\u4e0a\u8ff0\u547d\u4ee4\u5f8c\uff0cMongoDB\u5c07\u70ba\u201ctags\u201d\u5b57\u6bb5\u5275\u5efa\u4e00\u500b\u591a\u9375\u7d22\u5f15\u3002<\/p>\n<h2 id=\"%e6%9f%a5%e8%a9%a2%e7%a4%ba%e4%be%8b-CNCDbuZqAE\">\u67e5\u8a62\u793a\u4f8b<\/h2>\n<p>\u4f7f\u7528\u591a\u9375\u7d22\u5f15\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u67e5\u8a62\u6578\u7d44\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u60f3\u67e5\u8a62\u6240\u6709\u5305\u542b\u7279\u5b9a\u6a19\u7c64\u7684\u6587\u6a94\uff1a<\/p>\n<pre><code>db.collection.find({ \"tags\": \"mongodb\" })<\/code><\/pre>\n<p>\u9019\u689d\u67e5\u8a62\u5c07\u5229\u7528\u6211\u5011\u4e4b\u524d\u5275\u5efa\u7684\u591a\u9375\u7d22\u5f15\uff0c\u5f9e\u800c\u63d0\u9ad8\u67e5\u8a62\u901f\u5ea6\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-CNCDbuZqAE\">\u7e3d\u7d50<\/h2>\n<p>\u591a\u9375\u7d22\u5f15\u662fMongoDB\u4e2d\u4e00\u500b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u5c0d\u5305\u542b\u6578\u7d44\u5b57\u6bb5\u7684\u6587\u6a94\u7684\u67e5\u8a62\u6548\u7387\u3002\u901a\u904e\u81ea\u52d5\u5275\u5efa\u548c\u9ad8\u6548\u67e5\u8a62\uff0c\u591a\u9375\u7d22\u5f15\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u66f4\u8f15\u9b06\u5730\u8655\u7406\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\u3002\u5728\u9078\u64c7\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u6642\uff0c\u4e86\u89e3\u9019\u4e9b\u7d22\u5f15\u7684\u7279\u6027\u548c\u4f7f\u7528\u5834\u666f\u5c07\u6709\u52a9\u65bc\u512a\u5316\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u5982\u679c\u60a8\u5c0d\u65bc\u6578\u64da\u5eab\u7684\u9078\u64c7\u548c\u914d\u7f6e\u6709\u9032\u4e00\u6b65\u7684\u9700\u6c42\uff0c\u60a8\u53ef\u4ee5\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u642d\u5efa\u60a8\u7684MongoDB\u74b0\u5883\uff0c\u4eab\u53d7\u66f4\u9ad8\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3MongoDB\u4e2d\u7684\u591a\u9375\u7d22\u5f15\uff0c\u63d0\u5347\u67e5\u8a62\u6548\u7387\uff0c\u512a\u5316\u6578\u64da\u5b58\u53d6\uff0c\u5be6\u73fe\u66f4\u9748\u6d3b\u7684\u6578\u64da\u7ba1\u7406\u548c\u5206\u6790\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-63712","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63712","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=63712"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63712\/revisions"}],"predecessor-version":[{"id":63713,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/63712\/revisions\/63713"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=63712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=63712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=63712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}