How to delete data from more than week ago?

Hello
I have a web scraper that runs everyday and I want to delete data from more than a week ago

This is my coin model so far

    const coinSchema = mongoose.Schema(
  {
    specNo: {
      type: String,
      required: true,
    },
    coinName: {
      type: String,
      required: true,
    },
    fullName: {
      type: String,
      required: false,
    },
    category: {
      type: String,
    },
    array: [
      {
        GradeName: String,
        PopulationCount: String,
        trend: { type: Number, default: 0 },
      },
    ],
  },
  {
    timestamps: true,
  }
);

and this is the delete function in my model:

>     coinSchema.statics.deleteOldData = async function () {
>   // delete old data
>   const today = new Date(Date.now());
>   today.setHours(0, 0, 0, 0);
>   const oneWeekAgo = new Date(Date.now());
>   const pastDate = oneWeekAgo.getDate() - 7;
>   oneWeekAgo.setDate(pastDate);
> 
>   await this.deleteMany({
>     createdAt: {
>       $gte: today,
>     }, // 16 < 17 wont delete it prevent duplicates for one day
>   });
>   await this.deleteMany({
>     createdAt: {
>       $lt: pastDate,
>     }, // from 1 week ago
>   });
> };

However the data from more than a week ago is still stored in my MongoDB, is there a reason for this?

1 Like

Welcome to the MongoDB Community Forums @Juls_Oh !

The getDate() method returns the day of the month for the specified date, so your date math doesn’t support what you are trying to do:

> oneWeekAgo = new Date(Date.now());
2021-08-22T00:57:28.369Z

> pastDate = oneWeekAgo.getDate() - 7;
15

A JavaScript Date object stores a datetime representation in milliseconds, you would want a calculation like:

>  oneWeekAgo = new Date(new Date() - (7 * 24 * 60 * 60 * 1000))
2021-08-15T00:57:52.909Z

For easier date math I’d recommend using a library like moment.js:

# Exactly 7 days ago
> moment().subtract(7,'days')
Moment<2021-08-15T11:02:26+10:00>

# Start of day 7 days ago (local timezone)
> moment().subtract
(7,'days').startOf('day')
Moment<2021-08-15T00:00:00+10:00>

# Start of day 7 days ago (UTC)
> moment().subtract(7,'days').startOf('day').utc()
Moment<2021-08-14T14:00:00Z>

Regards,
Stennie

2 Likes

I need to add a .toDate() method at the end to retrieve data from MongoDB, but when .toDate() changes from T00:00 to T07:00 for startOf(‘day’) (which is today). Shouldnt it be T00:00 ?

Hi @Juls_Oh,

Sorry, I forgot to include that in my examples. You will need toDate() to convert the Moment object to a JavaScript Date.

If you calculate start of day for local time and convert that to a JavaScript Date with toDate(), you’ll end up with a Date object in UTC which will be offset per your local timezone.

I think what you want may be:

> moment().subtract(7,'days').utc().startOf('day').toDate()
2021-08-16T00:00:00.000Z

If that isn’t what you’re looking for, can you share your specific Moment.js invocation and the expected output?

FYI there’s also a Moment Timezone extension if you want more convenient helpers for working with timezones.

Regards,
Stennie

Yes it is however utc() adds another extra day which is wrong in my situation. However if I remove utc() it adds 7hours to the time again.