MongoDB | Remove old data

Today we will be looking at how to remove old records in MongoDB based on X days. Before we do anything, we need to generate a list of documents for the database:

db.statistic.insertMany([
  {
    event: 'pageview',
    userId: 'user1',
    createdAt: new Date('July 1, 2022 14:10:00'),
  },
  ...
    // more documents here
  ...
  {
    event: 'pageview',
    userId: 'user10',
    createdAt: new Date('July 10, 2022 11:22:00'),
  },
]);

For this example, we have 10 documents. We need to remove documents that are older than X days :

db.statistic.remove({ 
  createdAt: {
    $lt: new Date(today - X days) 
  } 
});

and for sure we must write a Cron job to do this.

cron "0 0 0 * * *" do
  db.statistic.remove({ 
    createdAt: { 
      $lt: new Date(today - X days) 
    } 
  });
end

Start Cron at 12:00 AM every day. You can use npm package to do this.

npm install cron

example of cron.js:

const { CronJob } = require('cron');

const job = new CronJob(
  '0 0 0 * * *',
  (() => {
    db.statistic.remove({
      createdAt: {
        $lt: new Date(today - X days),
      },
    });
  }),
  null,
  true,
  'America/Los_Angeles',
);

Let’s automate it without programming Cron.

db.statistic.createIndex({
  createdAt: 1,
}, {
  expireAfterSeconds: 60 * 60 * 24 * 7,
});

That’s all we need to do. Simple, right? :)

For more information about MongoDB TTL indexes, please visit Expire Data from Collections by Setting TTL.