I want to create a script which is going to delete documents in collections but remain the collection itself (just make 0 documents inside a collection). When I run the script with node it just runs but didn't do anything at all. No errors.
Code:
var mongoClient = require('mongodb').MongoClient;
var url = 'mongodb+srv://username:[email protected]/<dbname>?retryWrites=true&w=majority';
mongoClient.connect(url, { useUnifiedTopology: true }, function(err, db) {
if (err) {
console.log('Sorry unable to connect to MongoDB Error:', err);
} else {
async function deleteListingsScrapedBeforeDate() {
result = await client.db("CryptoCurrencies").collection("who")
.remove({});
console.log(`${result.deletedCount} document(s) was/were deleted.`);
await deleteListingsScrapedBeforeDate();
}
}
});
Also is there a way to have a script which is going to delete all documents in multiple collections? Can I declare a collections like an array?