{"id":1094,"date":"2023-03-12T11:33:02","date_gmt":"2023-03-12T03:33:02","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1094"},"modified":"2023-04-29T11:27:53","modified_gmt":"2023-04-29T03:27:53","slug":"mongodb-index","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/mongodb-index\/","title":{"rendered":"MongoDB\u7d22\u5f15"},"content":{"rendered":"<p>\u7d22\u5f15\u901a\u5e38\u80fd\u591f\u6781\u5927\u7684\u63d0\u9ad8\u67e5\u8be2\u7684\u6548\u7387\uff0c\u5982\u679c\u6ca1\u6709\u7d22\u5f15\uff0cMongoDB\u5728\u8bfb\u53d6\u6570\u636e\u65f6\u5fc5\u987b\u626b\u63cf\u96c6\u5408\u4e2d\u7684\u6bcf\u4e2a\u6587\u4ef6\u5e76\u9009\u53d6\u90a3\u4e9b\u7b26\u5408\u67e5\u8be2\u6761\u4ef6\u7684\u8bb0\u5f55\u3002<\/p>\n<p>\u8fd9\u79cd\u626b\u63cf\u5168\u96c6\u5408\u7684\u67e5\u8be2\u6548\u7387\u662f\u975e\u5e38\u4f4e\u7684\uff0c\u7279\u522b\u5728\u5904\u7406\u5927\u91cf\u7684\u6570\u636e\u65f6\uff0c\u67e5\u8be2\u53ef\u4ee5\u8981\u82b1\u8d39\u51e0\u5341\u79d2\u751a\u81f3\u51e0\u5206\u949f\uff0c\u8fd9\u5bf9\u7f51\u7ad9\u7684\u6027\u80fd\u662f\u975e\u5e38\u81f4\u547d\u7684\u3002<\/p>\n<p>\u7d22\u5f15\u662f\u7279\u6b8a\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7d22\u5f15\u5b58\u50a8\u5728\u4e00\u4e2a\u6613\u4e8e\u904d\u5386\u8bfb\u53d6\u7684\u6570\u636e\u96c6\u5408\u4e2d\uff0c\u7d22\u5f15\u662f\u5bf9\u6570\u636e\u5e93\u8868\u4e2d\u4e00\u5217\u6216\u591a\u5217\u7684\u503c\u8fdb\u884c\u6392\u5e8f\u7684\u4e00\u79cd\u7ed3\u6784\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>ensureIndex() \u65b9\u6cd5<\/h2>\n<p>MongoDB\u4f7f\u7528 ensureIndex() \u65b9\u6cd5\u6765\u521b\u5efa\u7d22\u5f15\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<p>ensureIndex()\u65b9\u6cd5\u57fa\u672c\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-javascript\">> db.COLLECTION_NAME.ensureIndex({KEY:1})<\/code><\/pre>\n<p>\u8bed\u6cd5\u4e2d Key \u503c\u4e3a\u4f60\u8981\u521b\u5efa\u7684\u7d22\u5f15\u5b57\u6bb5\uff0c1\u4e3a\u6307\u5b9a\u6309\u5347\u5e8f\u521b\u5efa\u7d22\u5f15\uff0c\u5982\u679c\u4f60\u60f3\u6309\u964d\u5e8f\u6765\u521b\u5efa\u7d22\u5f15\u6307\u5b9a\u4e3a-1\u5373\u53ef\u3002<\/p>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<pre><code class=\"language-javascript\">> db.col.ensureIndex({&quot;title&quot;:1})<\/code><\/pre>\n<p>ensureIndex() \u65b9\u6cd5\u4e2d\u4f60\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u4f7f\u7528\u591a\u4e2a\u5b57\u6bb5\u521b\u5efa\u7d22\u5f15\uff08\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e2d\u79f0\u4f5c\u590d\u5408\u7d22\u5f15\uff09\u3002<\/p>\n<pre><code class=\"language-javascript\">> db.col.ensureIndex({&quot;title&quot;:1, &quot;description&quot;:-1})<\/code><\/pre>\n<p>ensureIndex() \u63a5\u6536\u53ef\u9009\u53c2\u6570\u5217\u8868\u5982\u4e0b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Parameter<\/th>\n<th style=\"text-align: left;\">Type<\/th>\n<th style=\"text-align: left;\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">background<\/td>\n<td style=\"text-align: left;\">Boolean<\/td>\n<td style=\"text-align: left;\">\u5efa\u7d22\u5f15\u8fc7\u7a0b\u4f1a\u963b\u585e\u5176\u5b83\u6570\u636e\u5e93\u64cd\u4f5c\uff0cbackground\u53ef\u6307\u5b9a\u4ee5\u540e\u53f0\u65b9\u5f0f\u521b\u5efa\u7d22\u5f15\uff0c<br \/>\u9ed8\u8ba4\u503c\u4e3afalse\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">unique<\/td>\n<td style=\"text-align: left;\">Boolean<\/td>\n<td style=\"text-align: left;\">\u5efa\u7acb\u7684\u7d22\u5f15\u662f\u5426\u552f\u4e00\u3002\u6307\u5b9a\u4e3atrue\u521b\u5efa\u552f\u4e00\u7d22\u5f15\u3002\u9ed8\u8ba4\u503c\u4e3afalse\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">name<\/td>\n<td style=\"text-align: left;\">String<\/td>\n<td style=\"text-align: left;\">\u7d22\u5f15\u7684\u540d\u79f0\u3002\u5982\u679c\u672a\u6307\u5b9a\uff0c<br \/>MongoDB\u7684\u901a\u8fc7\u8fde\u63a5\u7d22\u5f15\u7684\u5b57\u6bb5\u540d\u548c\u6392\u5e8f\u987a\u5e8f\u751f\u6210\u4e00\u4e2a\u7d22\u5f15\u540d\u79f0\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">dropDups<\/td>\n<td style=\"text-align: left;\">Boolean<\/td>\n<td style=\"text-align: left;\">\u5728\u5efa\u7acb\u552f\u4e00\u7d22\u5f15\u65f6\u662f\u5426\u5220\u9664\u91cd\u590d\u8bb0\u5f55\uff0c\u6307\u5b9a true \u521b\u5efa\u552f\u4e00\u7d22\u5f15\u3002\u9ed8\u8ba4\u503c\u4e3a false\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">sparse<\/td>\n<td style=\"text-align: left;\">Boolean<\/td>\n<td style=\"text-align: left;\">\u5bf9\u6587\u6863\u4e2d\u4e0d\u5b58\u5728\u7684\u5b57\u6bb5\u6570\u636e\u4e0d\u542f\u7528\u7d22\u5f15\u3002\u5982\u679c\u8bbe\u7f6e\u4e3atrue\uff0c<br \/>\u5728\u7d22\u5f15\u5b57\u6bb5\u4e2d\u4e0d\u4f1a\u67e5\u8be2\u51fa\u4e0d\u5305\u542b\u5bf9\u5e94\u5b57\u6bb5\u7684\u6587\u6863\u3002\u9ed8\u8ba4\u503c\u4e3a false\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">expireAfterSeconds<\/td>\n<td style=\"text-align: left;\">Integer<\/td>\n<td style=\"text-align: left;\">\u6307\u5b9a\u4e00\u4e2a\u4ee5\u79d2\u4e3a\u5355\u4f4d\u7684\u6570\u503c\uff0c\u5b8c\u6210 TTL\u8bbe\u5b9a\uff0c\u8bbe\u5b9a\u96c6\u5408\u7684\u751f\u5b58\u65f6\u95f4\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">v<\/td>\n<td style=\"text-align: left;\">index version<\/td>\n<td style=\"text-align: left;\">\u7d22\u5f15\u7684\u7248\u672c\u53f7\u3002\u9ed8\u8ba4\u7684\u7d22\u5f15\u7248\u672c\u53d6\u51b3\u4e8eMongoDB\u521b\u5efa\u7d22\u5f15\u65f6\u8fd0\u884c\u7684\u7248\u672c\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">weights<\/td>\n<td style=\"text-align: left;\">Document<\/td>\n<td style=\"text-align: left;\">\u7d22\u5f15\u6743\u91cd\u503c\uff0c\u6570\u503c\u5728 1 \u5230 99,999 \u4e4b\u95f4\uff0c<br \/>\u8868\u793a\u8be5\u7d22\u5f15\u76f8\u5bf9\u4e8e\u5176\u4ed6\u7d22\u5f15\u5b57\u6bb5\u7684\u5f97\u5206\u6743\u91cd\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">default_language<\/td>\n<td style=\"text-align: left;\">String<\/td>\n<td style=\"text-align: left;\">\u5bf9\u4e8e\u6587\u672c\u7d22\u5f15\uff0c\u8be5\u53c2\u6570\u51b3\u5b9a\u505c\u7528\u8bcd\u53ca\u8bcd\u5e72\u548c\u8bcd\u5668\u7684\u89c4\u5219\u7684\u5217\u8868\u3002\u9ed8\u8ba4\u4e3a\u82f1\u8bed\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">language_override<\/td>\n<td style=\"text-align: left;\">String<\/td>\n<td style=\"text-align: left;\">\u5bf9\u4e8e\u6587\u672c\u7d22\u5f15\uff0c\u8be5\u53c2\u6570\u6307\u5b9a\u5305\u542b\u5728\u6587\u6863\u4e2d\u7684\u5b57\u6bb5\u540d\uff0c<br \/>\u8bed\u8a00\u8986\u76d6\u9ed8\u8ba4\u7684language\uff0c\u9ed8\u8ba4\u503c\u4e3alanguage\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<p>\u5728\u540e\u53f0\u521b\u5efa\u7d22\u5f15\uff1a<\/p>\n<pre><code class=\"language-javascript\">db.values.ensureIndex({open: 1, close: 1}, {background: true})<\/code><\/pre>\n<p>\u901a\u8fc7\u5728\u521b\u5efa\u7d22\u5f15\u65f6\u52a0 <code>background:true<\/code> \u7684\u9009\u9879\uff0c\u8ba9\u521b\u5efa\u5de5\u4f5c\u5728\u540e\u53f0\u6267\u884c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7d22\u5f15\u901a\u5e38\u80fd\u591f\u6781\u5927\u7684\u63d0\u9ad8\u67e5\u8be2\u7684\u6548\u7387\uff0c\u5982\u679c\u6ca1\u6709\u7d22\u5f15\uff0cMongoDB\u5728\u8bfb\u53d6\u6570\u636e\u65f6\u5fc5\u987b\u626b\u63cf\u96c6\u5408\u4e2d\u7684\u6bcf\u4e2a\u6587\u4ef6\u5e76\u9009\u53d6\u90a3\u4e9b\u7b26 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[300],"tags":[],"class_list":["post-1094","post","type-post","status-publish","format-standard","hentry","category-mongodb"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1094"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1094\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}