Docs Menu
Docs Home
/
MongoDB for VS Code
/ /

Delete Documents with VS Code

On this page

  • Prerequisites
  • Delete One Document
  • Delete Many Documents

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

  • Use the deleteOne() method to delete one document.

  • Use the deleteMany() method to delete more than one document.

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

  • Create a connection to a MongoDB deployment.

  • Activate the connection to the MongoDB deployment.

  • Open a MongoDB Playground.

  • Create Documents with VS Code or create documents in a collection using a different method..

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. VS Code Extension splits your Playground and outputs the results of your Playground in the Playground Results.json pane. If you disabled split-view, VS Code Extension outputs the results of your Playground in a new tab.

To run 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, VS Code Extension splits your Playground and outputs the following document in the Playground Results.json pane. If you disabled split-view, VS Code Extension outputs the following document in a new tab. If you manually move your playground results, VS Code Extension 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. VS Code Extension splits your Playground and outputs the results of your Playground in the Playground Results.json pane. If you disabled split-view, VS Code Extension outputs the results of your Playground in a new tab.

To run 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, VS Code Extension splits your Playground and outputs the following document in the Playground Results.json pane. If you disabled split-view, VS Code Extension outputs the following document in a new tab. If you manually move your playground results, VS Code Extension displays the results in that tab.

{
acknowleged: 1,
deletedCount: 3
}

Back

Update