<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TTL | SDT 攻城獅區</title>
	<atom:link href="https://sdt.hameba.tw/tag/ttl/feed/" rel="self" type="application/rss+xml" />
	<link>https://sdt.hameba.tw</link>
	<description>由Steven, Der, Ted 三位高級打字員所組成，是三位工程師(攻城獅)所維護的技術分享平台，或許偶爾會分享一些日常，有任何問題或是錯誤的部分，歡迎留言告訴我們！</description>
	<lastBuildDate>Sat, 04 Jan 2020 02:09:38 +0800</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.3</generator>

<image>
	<url>https://sdt.hameba.tw/wp-content/uploads/2020/02/hameba_favicon-150x150.png</url>
	<title>TTL | SDT 攻城獅區</title>
	<link>https://sdt.hameba.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>MongoDB 自動刪除過期數據，TTL 索引時效性功能</title>
		<link>https://sdt.hameba.tw/438/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mongodb-%25e8%2587%25aa%25e5%258b%2595%25e5%2588%25aa%25e9%2599%25a4%25e9%2581%258e%25e6%259c%259f%25e6%2595%25b8%25e6%2593%259a%25ef%25bc%258cttl-%25e7%25b4%25a2%25e5%25bc%2595%25e6%2599%2582%25e6%2595%2588%25e6%2580%25a7%25e5%258a%259f%25e8%2583%25bd</link>
				<comments>https://sdt.hameba.tw/438/#respond</comments>
				<pubDate>Sat, 04 Jan 2020 02:09:38 +0000</pubDate>
		<dc:creator><![CDATA[Hsu Steven]]></dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[TTL]]></category>

		<guid isPermaLink="false">https://sdt.hameba.tw/?p=438</guid>
				<description><![CDATA[<p>TTL索引是一種特殊索引，通過這種索引MongoDB會自動重新設置集合的文檔。這對於某些類型的信息來說是一個很 &#8230; </p>
<p class="link-more"><a href="https://sdt.hameba.tw/438/" class="more-link">閱讀全文<span class="screen-reader-text">〈MongoDB 自動刪除過期數據，TTL 索引時效性功能〉</span></a></p>
The post <a href="https://sdt.hameba.tw/438/">MongoDB 自動刪除過期數據，TTL 索引時效性功能</a> first appeared on <a href="https://sdt.hameba.tw">SDT 攻城獅區</a>.]]></description>
								<content:encoded><![CDATA[<p>TTL索引是一種特殊索引，通過這種索引MongoDB會自動重新設置集合的文檔。這對於某些類型的信息來說是一個很理想的特性，例如機器生成的事件數據，日誌，會話信息等，這些數據都只需要在數據庫中保存有限時間。</p>
<p>使用官方方式及網上很多提供方式，會以索引的方式進行時間到期自動刪除數據的方式。</p>
<p><span id="more-438"></span></p>
<p>環境:MongoDB v4.2.0</p>
<p>但是作者實際測試過後，發現透過索引方式好像只能限定最多3600秒以內。</p>
<p>如官方語法</p><pre class="crayon-plain-tag">db.eventlog.createIndex( { "expireAt": 1 }, { expireAfterSeconds: 3600 } )</pre><p>Schema設定</p><pre class="crayon-plain-tag">expireAt: {
  type: Date,
  default: Date.now,
  index: { expires: '1d' }
}</pre><p>這種方式發現index並非會自行建立，但是將expires設定於3600秒就會建立索引</p>
<p>最後作者只能使用以前看過一篇的技巧以function方式帶入，</p>
<p>express session connect mongo 也是直接將expires時間加上，</p>
<p>可參考此方式解決</p><pre class="crayon-plain-tag">expireAt: {
 type: Date,
 default: () =&gt; { return new Date(+new Date() + 7 * 24 * 60 * 60 * 1000 ) },
 required: true,
 expires: 1
}</pre><p>以現在時間 增加 7 * 24 * 60 * 60 * 1000 (7天) 毫秒為單位。</p>
<p>到期後約 3~5分鐘 就自動消失，也不是馬上refresh就消失，</p>
<p>以上述到期算法大概會約 <span style="color: #ff0000">7天又五分鐘</span>後消失，所以如果想要準確7天就消失可以自行減300,000毫秒</p>The post <a href="https://sdt.hameba.tw/438/">MongoDB 自動刪除過期數據，TTL 索引時效性功能</a> first appeared on <a href="https://sdt.hameba.tw">SDT 攻城獅區</a>.]]></content:encoded>
							<wfw:commentRss>https://sdt.hameba.tw/438/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
							</item>
	</channel>
</rss>
