Docs Home → Develop Applications → MongoDB Drivers → Node.js
Delete a Document
Overview
In this section, we show you how to call the write operations to remove documents from a collection in your MongoDB database.
Delete
If you want to remove existing documents from a collection, you can
use deleteOne()
to remove one document or deleteMany()
for one or
more documents. These methods accept a query document that matches the
documents you want to delete.
You can specify the document or documents to be deleted by the
deleteOne()
or deleteMany()
write operations in a JSON object as
follows:
const doc = { pageViews: { $gt: 10, $lt: 32768 } };
To delete the first matching document using the deleteOne()
method or
to delete all matching documents using the deleteMany()
method, pass the
document as the method parameter:
const deleteResult = await collection.deleteOne(doc); const deleteManyResult = await collection.deleteMany(doc);
You can print the number of documents deleted by the operation by
accessing the deletedCount
field of the result for each of the
method calls above as follows:
console.dir(deleteResult.deletedCount); console.dir(deleteManyResult.deletedCount);
Upon successful delete, these statements should print the number of documents deleted by the associated operation.
For fully runnable examples and additional information on the available options, see our usage examples for deleteOne() and deleteMany().