Docs Menu
Docs Home
/ / /
Node.js Driver
/ / /

Delete Documents

On this page

  • Overview
  • Delete

In this section, we show you how to call the write operations to remove documents from a collection in your MongoDB database.

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 myColl.deleteOne(doc);
const deleteManyResult = await myColl.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);

If the delete operation is successful, these statements print the number of documents deleted by the associated operation.

To see fully runnable examples and more information on the available options, see the usage examples for deleteOne() and deleteMany().

← Generate Custom Values for

On this page