TTL not working

What’s up everyone.
My data looks like this:

  {
    _id: ObjectId("6253a1f8c0a752a7af2ffad2"),
    timestamp: 'Mon Apr 11 2022 03:35:20 GMT+0000 (Coordinated Universal Time)',
    counts: { enter: 3, exit: 0, stay: 175 },
    enter: [ '1234', '24' ],
    exit: [],
    stay: [
      '1162',   '1180',   '1488',   '1494',   '1586',   '1622',   '1626' ],
    __v: 0
  }

A new item is added every minute. Just experimenting with creating an index I did this: db.collectionName.createIndex({timestamp: 1}, {expireAfterSeconds: 5})

And when I do getIndexes():

[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  {
    v: 2,
    key: { timestamp: 1 },
    name: 'timestamp_1',
    expireAfterSeconds: 5
  }
]

But it’s not working… data is just added every minute, nothing is getting erased. I’m using the new shell to do this. The server is running on Heroku, the database is on the cloud.
How do you do this? When I do get to configure it, I’d like for the data to be dropped every 2 hours or so.
Thanks.

I found the issue. I redefined the date type on the mongoose model, now the data looks like this and it works:
timestamp: ISODate("2022-04-11T04:02:30.648Z")

I’ve been learning how NOT to use mongoose for a few days. I hope it is a good decision!
I’ll leave this for the records…

4 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.