How to delete all items from MongoDB collection

when using itMongoDBYou may need to delete all items from the collection.

You can calldeleteManyThe collection method, passing an empty object.

like this:

yourcollection.deleteMany({})

This is a complete example:

const mongo = require('mongodb').MongoClient
const url = 'mongodb://localhost:27017'
let db, jobs

mongo.connect( url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => { if (err) { console.error(err) return } db = client.db(‘jobs’) jobs = db.collection(‘jobs’)

<span style="color:#a6e22e">jobs</span>.<span style="color:#a6e22e">deleteMany</span>({})

} )


More database tutorials: