Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

489
Views
Delete all records older than 3 months from current date Mongodb node.js

In my application I have activity logs of users stored in my DB (mongoDB), Since a lot of logs are collected within 3 months I want to Delete all the activity log records older than 3 months from current date. I using Node.js for my backend. I want to run this function everyday automatically. Please help me with this.

This is the Log model

const logSchema = new mongoose.Schema(
  {
    activityType: {
      type: String,
      require: true,
      enum: {
        values: [
          'create',
          'update',
          'delete',
          'login',
          'logout',
          'passwordReset',
        ],
        message: 'Wrong activity type!',
      },
    },
    activitySubject: {
      type: Object,
      require: true,
    },
    activityPerformedUserId: {
      type: String,
      require: true,
    },
  },
  { timestamps: true }
);

const Log = mongoose.model('log', logSchema);

module.exports = Log;

This is how my activity log document looks like.

{
  "_id": "62788f467a6d842face8a698",
  "activityType": "delete",
  "activitySubject": {
  "id": "627557836752492b6c7ff515",
  "name": "S Thomson",
  "nic": "78456123442"
  },
  "activityPerformedUserId": "6276ab95eb9e634310ea37f8",
  "createdAt": "2022-05-09T03:49:26.510Z",
  "updatedAt": "2022-05-09T03:49:26.510Z"
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Here it would be like

const a =  {
  "_id": "62788f467a6d842face8a698",
  "activityType": "delete",
  "activitySubject": {
    "id": "627557836752492b6c7ff515",
    "name": "S Thomson",
    "nic": "78456123442"
  },
  "activityPerformedUserId": "6276ab95eb9e634310ea37f8",
  "createdAt": "2022-05-09T03:49:26.510Z",
  "updatedAt": "2022-05-09T03:49:26.510Z"
}

Try this:

a.splice(a.findIndex(e => new Date(e.createdAt + 2592000000 ) > new Date()),1);
console.log(a);
over 4 years ago · Santiago Trujillo Report

0

The best feature of MongoDB which deletes the record to granular level precision is TTL

Create a field on which the entire document will be removed after that time has passed. For ex, if you want to delete a record on x day, y hour, z minute, then set the value of the TTL field to x y z.

In your case, you can set TTL field to createdAt + 90 days, so that it will remove it after 90 days from createdAt value

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!