Mongo TTL cleanup is not working
<p>I'm trying to get Mongo to remove documents with the TTL feature however without success. Have tried many things but mongo doesn't seem to clean up.</p>

<p>My index:</p>

<pre><code>    {
    "v" : 1,
    "key" : {
        "date" : 1
    },
    "name" : "date_1",
    "ns" : "history.history",
    "expireAfterSeconds" : 60
}
</code></pre>

<p>The date value from document:</p>

<pre><code>       "date" : "2016-09-29 11:08:46.461207",
</code></pre>

<p>Output from db.serverStatus().metrics.ttl:</p>

<pre><code>{ "deletedDocuments" : NumberLong(0), "passes" : NumberLong(29) }
</code></pre>

<p>Time output from db.serverStatus():</p>

<pre><code>"localTime" : ISODate("2016-09-29T11:19:45.345Z")
</code></pre>

<p>Only thing I suspect is the way I insert the value from Python. Could be that it's in some way wrong. I have a JSON document which contains the following element:</p>

<pre><code>"date": str(datetime.utcnow()), 
</code></pre>

<p>Any clues where the problem might lay?</p>

<p>Thanks,
Janis </p>

<p>As you have guessed, the problem is in how you insert the date value. I'll quote the <a href="https://docs.mongodb.com/manual/core/index-ttl/" rel="nofollow">docs</a>:</p>

<blockquote>
  <p>If the indexed field in a document is not a date or an array that
  holds a date value(s), the document will not expire.</p>
</blockquote>

<p>You are casting the date to a string. If you are using the pymongo driver, he will handle datetimes nicely and convert it to MongoDB native <a href="https://docs.mongodb.com/manual/reference/bson-types/#date" rel="nofollow">Date type</a>.</p>

<p>This way, the following should work:</p>

<pre><code>"date": datetime.utcnow()
</code></pre>

python mongodb ttl