Delete Documents

On this page本页内容

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 outputs the results of your playground to the Output view in Visual Studio Code.

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, this operation outputs the following document to the Output view in Visual Studio Code:

{
  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 outputs the results of your playground to the Output view in Visual Studio Code.

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, this operation outputs the following document to the Output view in Visual Studio Code:

{
  acknowleged: 1,
  deletedCount: 3
}
Update DocumentsRun Aggregation Pipelines