I need to refresh my collection by deleting all existing documents and then repopulating them with new data from an API call. But when I try running the delete operations first, no new documents are created.
Below is a simplified version of my controller function:
let { data } = req.body
try {
let result = await Record.deleteMany({});
} catch (err) {
res.status(400).json('Failed to clear database');
}
for (let i in data) {
await Record.create(data[i]);
}
return res.status(200).json('Successfully Updated Collection')
How can I fix this issue? I just want to remove all existing documents in the collection and then immediately add the new ones.