I created an index as shown below and added a createdAt field to each new record added to the db. The records should be auto-deleted after 24 hours however I have waited days and nothing has been deleted.
db_connect.collection("records").createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )
Adding record to the database
// Add info to db to search
console.log("Adding info to database..");
const otherAddress = e.target.address.value;
const newRecord = {
holderAddress: this.props.account,
otherAddress: otherAddress,
date: new Date(),
data: encryptedData
}
await fetch("http://localhost:420/record/add", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(newRecord),
}).catch(error => {
window.alert(error);
});
Endpoint it calls:
recordRoutes.route("/record/add").post(function (req, response) {
let db_connect = dbo.getDb();
let myobj = {
holderAddress: req.body.holderAddress,
otherAddress: req.body.otherAddress,
data: req.body.data,
createdAt: req.body.date
};
db_connect.collection("records").insertOne(myobj, function (err, res) {
if (err) throw err;
response.json(res);
});
Below is a screenshot from MongoDB website confirming there is an index..
Any help is greatly appreciated!