Delete a document
suggest changeDelete a document with the property { greetings: 'Whut?' }. 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').deleteOne(
{ greetings: "Whut?" },
function (err, result) {
if (err) throw err;
client.close();
}
);
});
Collection method deleteOne()
db.collection(collection).deleteOne(filter, options, callback)
| Parameter | Type | Description |
|---|---|---|
filter |
object | 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