{"id":3421,"date":"2018-11-27T11:01:41","date_gmt":"2018-11-27T03:01:41","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3421"},"modified":"2018-11-27T11:01:41","modified_gmt":"2018-11-27T03:01:41","slug":"%e4%bd%bf%e7%94%a8-ttl-%e7%b4%a2%e5%bc%95%e5%ae%9a%e6%9c%9f%e6%b8%85%e7%90%86-mongodb-%e4%b8%ad%e6%9f%90%e4%b8%aa%e9%9b%86%e5%90%88%e7%9a%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3421","title":{"rendered":"\u4f7f\u7528 TTL \u7d22\u5f15\u5b9a\u671f\u6e05\u7406 MongoDB \u4e2d\u67d0\u4e2a\u96c6\u5408\u7684\u6570\u636e"},"content":{"rendered":"<p>\n\t\u6700\u8fd1\u6211\u5728\u5b66\u4e60 MongoDB\uff0c\u5728 MongoDB \u4e2d\u6587\u793e\u533a\u91cc\u770b\u5230\u4e86\u4e00\u4e2a\u6295\u7968\u6700\u591a\u7684\u5e16\u5b50\u3002\u6295\u7968\u6700\u591a\u7684\u4e5f\u5c31\u610f\u5473\u7740\u7528\u7684\u4eba\u591a\uff0c\u9047\u5230\u8fd9\u4e2a\u9700\u6c42\uff0c\u6709\u8fd9\u4e2a\u9700\u8981\u7684\u4eba\u4e5f\u591a\u3002\u6240\u4ee5\uff0c\u6211\u5c31\u9488\u5bf9\u6027\u7684\u91cd\u70b9\u7814\u7a76\u4e86\u4e00\u4e0b\u3002\u628a\u5982\u4f55\u5b9a\u671f\u6e05\u7406 mongo \u4e2d\u67d0\u4e2a\u96c6\u5408\u7684\u6570\u636e\u7ecf\u9a8c\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\n<\/p>\n<p>\n\t\u8981\u5b9e\u73b0\u5b9a\u65f6\u6e05\u7406\u8fc7\u671f\u6570\u636e\uff0c\u5c31\u9700\u8981\u521b\u5efa MongoDB \u7684 TTL\uff08Time To Live\uff09 \u7d22\u5f15\u3002\u521b\u5efa\u8be5\u7d22\u5f15\u540e\uff0cMongoDB \u6570\u636e\u5e93\u5c06\u5728\u540e\u53f0\u8d77\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u6bcf 60s \u89e6\u53d1\u4e00\u6b21\uff0c\u68c0\u67e5 MongoDB \u4e2d\u7684\u6570\u636e\uff0c\u68c0\u67e5\u8fc7\u671f\u6570\u636e\uff0c\u5e76\u6267\u884c\u5177\u4f53\u7684\u52a8\u4f5c\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u6211\u4eec\u4e00\u8d77\u770b\u4e00\u4e2a\u6848\u4f8b\uff1a\n<\/p>\n<pre class=\"brush:sql;\">\r\ndb.xttblog.createIndex({&quot;createDate&quot;:1},{expireAfterSeconds:3600});<\/pre>\n<p>\n\t\u89e3\u91ca\u4e00\u4e0b\uff0cxttblog \u662f\u96c6\u5408\u7684\u540d\u5b57\u3002createIndex \u662f\u521b\u5efa\u7d22\u5f15\uff0c3.0 \u4ee5\u524d\u7684\u7248\u672c\u521b\u5efa\u7d22\u5f15\u53ef\u4ee5\u4f7f\u7528 ensureIndex\uff0c\u4ece MongoDB 3.0\u5f00\u59cb ensureIndex \u88ab\u5e9f\u5f03\uff0c\u4eca\u540e\u90fd\u4ec5\u4ec5\u662f db.collection.createIndex \u7684\u4e00\u4e2a\u522b\u540d\u3002createDate \u662f\u96c6\u5408 xttblog \u4e2d\u7684\u5b57\u6bb5\uff0cTTL \u7d22\u5f15\u6700\u7ec8\u5c31\u662f\u521b\u5efa\u5728\u8be5\u5b57\u6bb5\u4e0a\uff0c\u4e3a Date \u65e5\u671f\u683c\u5f0f\u3002expireAfterSeconds \u662f\u8fc7\u671f\u65f6\u95f4\uff0c\u6839\u636e\u5b57\u6bb5\u7684\u540d\u79f0\u5c31\u53ef\u4ee5\u770b\u51fa\u6765\uff0c\u5b83\u662f\u591a\u5c11\u79d2\u540e\u8fc7\u671f\u3002\n<\/p>\n<p>\n\tTTL \u7d22\u5f15\u662f MongoDB \u4e2d\u7684\u4e00\u79cd\u7279\u6b8a\u7684\u7d22\u5f15\uff0c \u53ef\u4ee5\u652f\u6301\u6587\u6863\u5728\u4e00\u5b9a\u65f6\u95f4\u4e4b\u540e\u81ea\u52a8\u8fc7\u671f\u5220\u9664\uff0c\u76ee\u524d TTL \u7d22\u5f15\u53ea\u80fd\u5728\u5355\u5b57\u6bb5\u4e0a\u5efa\u7acb\uff0c\u5e76\u4e14\u5b57\u6bb5\u7c7b\u578b\u5fc5\u987b\u662f date \u7c7b\u578b\u6216\u8005\u5305\u542b\u6709 date \u7c7b\u578b\u7684\u6570\u7ec4\uff08\u5982\u679c\u6570\u7ec4\u4e2d\u5305\u542b\u591a\u4e2a date \u7c7b\u578b\u5b57\u6bb5\uff0c\u5219\u53d6\u6700\u65e9\u65f6\u95f4\u4e3a\u8fc7\u671f\u65f6\u95f4\uff09\u3002\u66f4\u591a\u8d44\u6599\u8bf7\u53c2\u8003\u5b98\u65b9\u8bf4\u660e\uff1a<a href=\"https:\/\/docs.mongodb.com\/v3.2\/core\/index-ttl\/\" name=\" TTL Indexes\" target=\"_blank\" title=\" TTL Indexes\">https:\/\/docs.mongodb.com\/v3.2\/core\/index-ttl\/<\/a>\u3002\u4ece\u8fd9\u91cc\u4e5f\u53ef\u4ee5\u770b\u51fa\uff0cTTL \u662f MongoDB 3.2 \u7248\u672c\u4ee5\u540e\u624d\u652f\u6301\u7684\u3002\n<\/p>\n<p>\n\t\u67e5\u770b\u7d22\u5f15\uff1a\n<\/p>\n<pre class=\"brush:sql;\">\r\ndb.xttblog.getIndexes();<\/pre>\n<p>\n\t\u7d22\u5f15\u4fe1\u606f\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:plain;\">\r\n[\r\n\t{\r\n        &quot;v&quot; : 1,\r\n        &quot;key&quot; : {\r\n                &quot;_id&quot; : 1\r\n        },\r\n        &quot;name&quot; : &quot;_id_&quot;,\r\n        &quot;ns&quot; : &quot;tt.t1&quot;\r\n\t},\r\n\t{\r\n        &quot;v&quot; : 1,\r\n        &quot;key&quot; : {\r\n                &quot;createDate&quot; : 1\r\n        },\r\n        &quot;name&quot; : &quot;createDate_1&quot;,\r\n        &quot;ns&quot; : &quot;tt.t1&quot;,\r\n        &quot;expireAfterSeconds&quot; : 3600\r\n\t}\r\n]<\/pre>\n<p>\n\t\u5982\u679c\u60f3\u66f4\u6539\u8fc7\u671f\u65f6\u95f4 expireAfterSeconds\uff0c\u53ef\u4ee5\u4f7f\u7528 collMod \u65b9\u6cd5\uff0c\u8981\u4e0d\u7136\u4f60\u53ea\u80fd\u53ea\u7528 dropIndex()\uff0c\u7136\u540e\u518d\u7528 createIndex() \u65b9\u6cd5\u91cd\u5efa\u7d22\u5f15\u4e86\u3002\u8fd9\u79cd\u91cd\u5efa\u7d22\u5f15\u7684\u65b9\u6cd5\u662f\u975e\u5e38\u4e0d\u63a8\u8350\u7684\uff0c\u56e0\u4e3a\u5728\u4ebf\u7ea7\u6570\u636e\u91cf\u4e0b\u662f\u5f88\u6050\u6016\u7684\u3002\n<\/p>\n<pre class=\"brush:sql;\">\r\ndb.runCommand( {collMod: &quot;xttblog&quot;, --- \u96c6\u5408\u540d\r\n\tindex: { keyPattern: { createDate: 1 }, --- createDate \u4e3a\u5177\u6709 TTL \u7d22\u5f15\u7684\u5b57\u6bb5\u540d\r\n\t\texpireAfterSeconds: 7200 --- \u4fee\u6539\u540e\u7684\u8fc7\u671f\u65f6\u95f4(\u79d2)\r\n\t}}\r\n)<\/pre>\n<p>\n\tTTL \u7d22\u5f15\u7684\u7f3a\u70b9\u4e5f\u5f88\u5927\uff0c\u6bd4\u5982\u53ea\u652f\u6301\u5355\u5217\u7d22\u5f15\u3001\u6bcf 60s \u68c0\u67e5\u4e00\u6b21\u3001\u4fee\u6539\u7d22\u5f15\u5f71\u54cd\u5927\u7b49\u7f3a\u70b9\u3002\u5982\u679c\u767d\u5929\u4e1a\u52a1\u6bd4\u8f83\u5fd9\uff0c\u662f\u5427\uff0cTTL \u7d22\u5f15\u5b9a\u65f6\u7684\u53bb\u5220\u9664\u6570\u636e\uff0c\u5bf9\u5e94\u4e0a\u4ebf\u7684\u6570\u636e\u91cf\u6027\u80fd\u5f71\u54cd\u662f\u975e\u5e38\u5927\u7684\u3002\u6240\u4ee5\u5b9a\u65f6\u6e05\u7406\u4e00\u822c\u8981\u653e\u5728\u591c\u665a\u65f6\u95f4\uff0c\u4e1a\u52a1\u91cf\u8f83\u5c11\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u3002\n<\/p>\n<pre class=\"brush:sql;\">\r\ndb.xttblog.createIndex( { &quot;expireTime&quot;: 1 }, ---\u5b57\u6bb5\u540d\u79f0\r\n\t{ expireAfterSeconds: 0 }  ---\u8fc7\u671f\u65f6\u95f4\uff08\u5355\u4f4d\u79d2\uff09\r\n)\r\n\/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com  \r\ndb.xttblog.insert( {\r\n  &quot;expireTime&quot;: new Date(&#039;Jan 22, 2018 23:00:00&#039;), --- \u6b64\u6587\u6863\u5c06\u57282018-1-22\u768423\u70b9\u81ea\u52a8\u5220\u9664\r\n  &quot;logEvent&quot;: 2,\r\n  &quot;logMessage&quot;: &quot;Success!&quot;} \r\n)<\/pre>\n<p>\n\t\u4e0a\u9762\u7684\u505a\u6cd5\u5c31\u53ef\u4ee5\u5728\u6307\u5b9a\u65f6\u95f4\u81ea\u52a8\u5220\u9664\u6570\u636e\u4e86\u3002\n<\/p>\n<p>\n\tTTL \u7d22\u5f15\u867d\u7136\u597d\uff0c\u4f46\u4e5f\u6709\u5f88\u591a\u9650\u5236\u6761\u4ef6\u3002\n<\/p>\n<ul>\n<li>\n\t\tTTL \u7d22\u5f15\u662f\u5355\u5b57\u6bb5\u7d22\u5f15\uff0c\u6df7\u5408\u7d22\u5f15\u4e0d\u652f\u6301 TTL\uff0c\u5e76\u4e14\u4e5f\u4f1a\u5ffd\u7565 expireAfterSeconds \u5c5e\u6027\n\t<\/li>\n<li>\n\t\t\u5728 _id \u4e3b\u952e\u4e0a\u4e0d\u80fd\u5efa\u7acb TTL \u7d22\u5f15\n\t<\/li>\n<li>\n\t\t\u5728 capped collection \u4e2d\u4e0d\u80fd\u5efa\u7acb TTL \u7d22\u5f15\uff0c\u56e0\u4e3a MongoDB \u4e0d\u80fd\u4ece capped collection \u4e2d\u5220\u9664\u6587\u6863\n\t<\/li>\n<li>\n\t\t\u4e0d\u80fd\u4f7f\u7528 createIndex() \u53bb\u66f4\u6539\u5df2\u7ecf\u5b58\u5728\u7684 TTL \u7d22\u5f15\u7684 expireAfterSeconds \u503c\uff0c\u5982\u679c\u60f3\u66f4\u6539 expireAfterSeconds\uff0c\u53ef\u4ee5\u4f7f\u7528 collMod \u547d\u4ee4\uff0c\u5426\u5219\u4f60\u53ea\u80fd\u5220\u9664\u7d22\u5f15\uff0c\u7136\u540e\u91cd\u5efa\u4e86\n\t<\/li>\n<li>\n\t\t\u4e0d\u80fd\u5728\u5df2\u6709\u7d22\u5f15\u7684\u5b57\u6bb5\u4e0a\u518d\u521b\u5efa TTL \u7d22\u5f15\u4e86\uff0c\u5982\u679c\u4f60\u60f3\u628a\u975e TTL \u7d22\u5f15\u6539\u4e3a TTL \u7d22\u5f15\uff0c\u90a3\u5c31\u53ea\u80fd\u5220\u9664\u91cd\u5efa\u7d22\u5f15\u4e86\n\t<\/li>\n<\/ul>\n<p>\n\t<img decoding=\"async\" alt=\"\u5982\u4f55\u5b9a\u671f\u6e05\u7406 mongo \u4e2d\u67d0\u4e2a\u96c6\u5408\u7684\u6570\u636e\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/mongodb-ttl-vmstat.png\" style=\"width: 535px;height: 511px\" \/>\n<\/p>\n<p>\n\t\u6709\u4eba\u6d4b\u8bd5\u8fc7 TTL \u7d22\u5f15\u5728\u4ebf\u7ea7\u522b\u96c6\u5408\u4e2d\u5220\u9664 140 \u4e07\u8fc7\u671f\u6570\u636e\u7684\u6d88\u8017\u3002\u5220\u9664\u64cd\u4f5c\u6574\u4e2a\u8fc7\u7a0b\u5728 90 \u79d2\u5de6\u53f3\u5b8c\u6210\uff1bCPU \u6700\u9ad8\u5360\u7528 90%\uff0c\u5e73\u5747\u5728 50%\uff0c\u5185\u5b58\u5360\u7528 3G\u3002\u6d4b\u8bd5\u914d\u7f6e\uff0cOS\uff1aVm\u865a\u62df\u673a\uff0cCPU: 4 \u6838\uff0c\u5185\u5b58\uff1a8G\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3421\">\u4f7f\u7528 TTL \u7d22\u5f15\u5b9a\u671f\u6e05\u7406 MongoDB \u4e2d\u67d0\u4e2a\u96c6\u5408\u7684\u6570\u636e<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u6211\u5728\u5b66\u4e60 MongoDB\uff0c\u5728 MongoDB \u4e2d\u6587\u793e\u533a\u91cc\u770b\u5230\u4e86\u4e00\u4e2a\u6295\u7968\u6700\u591a\u7684\u5e16\u5b50\u3002\u6295\u7968\u6700\u591a\u7684\u4e5f\u5c31\u610f\u5473\u7740\u7528\u7684 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1578,1577,155,156,541],"class_list":["post-3421","post","type-post","status-publish","format-standard","hentry","category-java","tag-mongodb","tag-nosql","tag-sql","tag-156","tag-541"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3421"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3421\/revisions"}],"predecessor-version":[{"id":3422,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3421\/revisions\/3422"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}