title: MongoDB NodeJS always expiring after one minute
body: <p>I am using the <a href="https://www.npmjs.com/package/mongodb" rel="nofollow noreferrer">MongoDB</a> npm module in my node app and I have this code to create a new object in the collection with a <a href="https://docs.mongodb.com/manual/core/index-ttl/" rel="nofollow noreferrer">TTL index</a>:</p>
  
  <pre><code>var utcDate = new Date();
  var emailObj = {email: emailAddress, createdAt: utcDate};
  dbo.collection("initialEmail").createIndex({ "createdAt": 1 }, { expireAfterSeconds: 3600 });
  dbo.collection("initialEmail").insertOne(emailObj, function(err, response) {
      ...
  });
  </code></pre>
  
  <p>However, the entry always deletes after about one minute (instead of the 1 hour I set it to be). </p>
  
  <p>I got from my research that mongo runs its script to delete about every <a href="https://docs.mongodb.com/manual/core/index-ttl/#timing-of-the-delete-operation" rel="nofollow noreferrer">one minute</a>. This makes me think that for some reason my mongo server timezone is off from my node server timezone, therefore making the mongo expiration time of the object created already an hour before the current mongo server time.</p>
  
  <p>For the <code>utcDate</code> variable, I have tried:</p>
  
  <ul>
  <li><code>new Date( moment.utc().format() );</code></li>
  <li><code>new Date( moment().valueOf() );</code></li>
  <li><code>new Date(Date.now() - new Date().getTimezoneOffset() * 60 * 1000);</code></li>
  </ul>
  
  <p>But there was no effect. All of these make the object delete after about 1 minute.</p>
  
  <p>I have also tried: (for <code>utcDate</code>)</p>
  
  <ul>
  <li><code>moment().valueOf();</code></li>
  <li><code>Date.now() - new Date().getTimezoneOffset() * 60 * 1000;</code></li>
  </ul>
  
  <p>But these cause the type of <code>createdAt</code> when viewing the object as JSON to be an int, and the object never gets deleted, (even after waiting many minutes after expected expiration).</p>
  
  <p>How can I ensure the object inserted will always last at least an hour?</p>