Lucas Neves
Lucas Neves

Reputation: 65

MongoDB node.js: index created ignoring TTL

I'm trying to create an index with TTL using the MongoDB driver for Node.js and a Mongo server hosted at mLab.

Node version 9.3.0.
Driver version 3.0.0.rc0
mongod version: 3.4.10 (MMAPv1)

Code in node.js:

var processCollection;

async function init (options) {
  processCollection = await options.db.collection('processes');

  await processCollection.dropIndexes();
  await processCollection.createIndex(
    { 'modified': 1 },
    { expireAfterSeconds: 3600 }
  );
}

Results in DB:

db['system.indexes'].find()
{
  "v": 2,
  "key": {
    "modified": 1
  },
  "name": "modified_1",
  "ns": "e-consular.processes"
}

The option expireAfterSeconds is missing in the resulting index. What am I doing wrong?

Upvotes: 0

Views: 472

Answers (1)

tfogo
tfogo

Reputation: 1436

Collection.createIndex is broken in versions 3.0.0rc0 and 3.0.0 of the Node mongodb driver. It will ignore the options object argument.

This was fixed in version 3.0.1 of the driver. (You can see the fix here).

Update your driver to the latest version (e.g. npm i [email protected]) and it should work as expected.

Upvotes: 2

Related Questions