I created a time-series collection using MongoDB database, and I expected the data to be automatically deleted after 2 minutes, but it did not happen. I checked the database and found that the data still exists.
I created a time-series
collection using MongoDB database and I need to delete the data in the nonce
field after 2 minutes. I created it using MongoDB Compass, and my parameters are as follows:
Collection Name: nonce
timeField: ts
granularity: seconds
expireAfterSeconds: 120
However, when I insert data using mongoose in node.js, the data is not deleted after 2 minutes.
Here is the code I am using:
const nonceSchema = new mongoose.Schema({
nonce: String,
ts: Date,
})
const nonceModel = mongoose.model('nonce', nonceSchema,'nonce');
await nonceModel.create({
nonce: nonce,
ts: new Date(),
});
This code correctly inserts data, but the data is not automatically deleted.
[{
"ts": {
"$date": "2023-04-29T06:28:21.345Z"
},
"nonce": "R8tYOm15zEu1BupI",
"_id": {
"$oid": "644cb90554447c607a4a5a37"
},
"__v": 0
},{
"ts": {
"$date": "2023-04-29T06:28:47.098Z"
},
"nonce": "fFHGD8h6LwUDFIWo",
"_id": {
"$oid": "644cb91f54447c607a4a5a3b"
},
"__v": 0
}]
What should I do? Thank you for your help.