Delete Documents with VS Code
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.
Prerequisites
If you have not done so already, you must complete the following prerequisites before you can delete documents with a MongoDB Playground:
Create Documents with VS Code or create documents in a collection using a different method..
Delete One Document
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.
Example
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.
The following example:
Switches to the
test
database.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 }
Delete Many Documents
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.
Example
To run this example, start with a blank MongoDB Playground by clearing the template Playground if it is loaded.
The following example:
Switches to the
test
database.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 }