Navigation

Delete Documents

You delete documents in a collection using the MongoDB CRUD Operators in a MongoDB Playground:

If you have not done so already, you must complete the following prerequisites before you can delete documents with a MongoDB Playground:

To delete one document, use the following syntax in your Playground:

db.collection.deleteOne(
<filter>,
{
writeConcern: <document>,
collation: <document>
}
)

For a detailed description of this method's parameters, see deleteOne() in the MongoDB Manual.

To run your Playground, press the Play Button at the top right of the Playground View. MongoDB for VS Code splits your Playground and outputs the results of your Playground in the Playground Results.json pane. If you disabled split-view, MongoDB for VS Code outputs the results of your Playground in a new tab.

To use this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.

The following example:

  1. Switches to the test database.
  2. Deletes one document in the test.sales collection that matches the query.
use("test");
db.sales.deleteOne(
{ "_id" : 1 }
);

When you press the Play Button, MongoDB for VS Code splits your Playground and outputs the following document in the Playground Results.json pane. If you disabled split-view, MongoDB for VS Code outputs the following document in a new tab. If you manually move your playground results, MongoDB for VS Code displays the results in that tab.

{
acknowleged: 1,
deletedCount: 1
}

To delete many documents, use the following syntax in your Playground:

db.collection.deleteMany(
<filter>,
{
writeConcern: <document>,
collation: <document>
}
)

For a detailed description of this method's parameters, see deleteMany() in the MongoDB Manual.

To run your Playground, press the Play Button at the top right of the Playground View. MongoDB for VS Code splits your Playground and outputs the results of your Playground in the Playground Results.json pane. If you disabled split-view, MongoDB for VS Code outputs the results of your Playground in a new tab.

To use this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.

The following example:

  1. Switches to the test database.
  2. Deletes all documents in the test.sales collection that match the query.
use("test");
db.sales.deleteMany(
{ "item" : "abc" }
);

When you press the Play Button, MongoDB for VS Code splits your Playground and outputs the following document in the Playground Results.json pane. If you disabled split-view, MongoDB for VS Code outputs the following document in a new tab. If you manually move your playground results, MongoDB for VS Code displays the results in that tab.

{
acknowleged: 1,
deletedCount: 3
}
Give Feedback