Delete multiple documents
suggest changeDelete every document whose farewell property is 'okay'. Since driver 3.0, MongoClient.connect passes a client, not a Db.
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
MongoClient.connect(url, function (err, client) {
if (err) throw err;
var db = client.db('test');
db.collection('myCollection').deleteMany(
{ farewell: "okay" },
function (err, result) {
if (err) throw err;
client.close();
}
);
});
Collection method deleteMany()
db.collection(collection).deleteMany(filter, options, callback)
| Parameter | Type | Description |
|---|---|---|
filter |
document | A document that selects which documents to delete |
options |
object | (optional) Extra settings (default: null) |
callback |
function | Called when the operation finishes |
The callback is error-first:
err: anErrorif the operation failed, otherwisenullresult: the driver's delete result, includingdeletedCount. It is not the database client.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents