I want to delete entries after e.g. 24 hours, but the entries are already deleted after 2 minutes.
I do the whole thing via mongoose schema.
I use nodejs with mongoose.
const expTime = 24 * 60 * 60;
const schema = new Schema({
a: {
type: String,
required: true
},
b: {
type: String
}
}, { timestamps: true });
schema.index({ createdAt: 1 }, { expireAfterSeconds: expTime });
const model = mongoose.model("example", schema);
//...
model.create({ a, b, "createdAt": new Date() })
.then(//...)
.catch((e) => {
console.error(e);
//...
});
This is the output when I query the data every minute
10:34:05 AM {a: '...', b: '...', createdAt: '2023-03-09T09:34:05.314Z', updatedAt: '2023-03-09T09:34:05.425Z'}
10:35:00 AM {a: '...', b: '...', createdAt: '2023-03-09T09:34:05.314Z', updatedAt: '2023-03-09T09:34:05.425Z'}
10:36:00 AM {a: '...', b: '...', createdAt: '2023-03-09T09:34:05.314Z', updatedAt: '2023-03-09T09:34:05.425Z'}
10:37:00 AM undefined
The same happens when I set the ttl to only 5 minutes.
Can someone tell me why this is not deleted after 24 hours?
I have already tried all possible solutions, without any of them working :/
for example these: Setting expiry time for a collection in mongodb using mongoose
I use
- mongo 6.0.4
- node 19.7.0
-
- mongoose 7.0.1
Edit
By testing the wrong ttl was deposited
db.getCollection("example").getIndexes().filter(x => x.expireAfterSeconds)
returned:
[
{
v: 2,
key: { expireAt: 1 },
name: 'expireAt_1',
background: true,
expireAfterSeconds: 300
},
{
v: 2,
key: { createdAt: 1 },
name: 'createdAt_1',
background: true,
expireAfterSeconds: 120
}
]
After I have set up the database again it is correct
[
{
v: 2,
key: { createdAt: 1 },
name: 'createdAt_1',
background: true,
expireAfterSeconds: 86400
}
]