Docs Menu
Docs Home
/ / /
Rust Driver
/ / /

Delete Documents

On this page

  • Overview
  • Sample Data for Examples
  • Delete Operations
  • Parameters
  • Return Value
  • delete_many() Example
  • Additional Information
  • API Documentation

In this guide, you can learn how to remove documents from your MongoDB collections using delete operations.

This guide includes the following sections:

  • Sample Data for Examples presents the sample data that is used by the delete operation example

  • Delete Operations describes how to use the driver to execute delete operations

  • Additional Information provides links to resources and API documentation for types and methods mentioned in this guide

The example in this guide uses the following sample documents. Each document represents an item in a store's inventory and contains information about its categorization and unit price:

{ "item": "trowel", "category": "garden", "unit_price": 9.89 },
{ "item": "placemat", "category": "kitchen", "unit_price": 3.19 },
{ "item": "watering can", "category": "garden", "unit_price": 11.99 }

The Rust driver provides the delete_one() and delete_many() methods to perform delete operations.

The delete_one() and delete_many() methods take a query filter as a parameter. A query filter consists of the fields and values that form criteria for documents to match.

You can also optionally pass a DeleteOptions type as a parameter to either method. You can specify settings in a DeleteOptions instance to configure the delete operation. To use default values for each setting, specify the value None as the options parameter.

Note

Instantiating Options

The Rust driver implements the Builder design pattern for the creation of many different types, including DeleteOptions. You can use each type's builder() method to construct an options instance by chaining option builder functions one at a time.

The following table describes settings that you can specify in a DeleteOptions instance:

Option
Description
collation
The collation to use when sorting results. To learn more about collations, see the Collations guide.

Type: Collation
Default: None
write_concern
The write concern for the operation. If you don't set this option, the operation inherits the write concern set for the collection. To learn more about write concerns, see Write Concern in the Server manual.

Type: WriteConcern
hint
The index to use for the operation. To learn more about indexes, see Indexes in the Server manual. This option is available only when connecting to MongoDB Server versions 4.4 and later.

Type: Hint
Default: None
let_vars
A map of parameters and values. These parameters can be accessed as variables in aggregation expressions. This option is available only when connecting to MongoDB Server versions 5.0 and later.

Type: Document
comment
An arbitrary Bson value tied to the operation to trace it through the database profiler, currentOp, and logs. This option is available only when connecting to MongoDB Server versions 4.4 and later.

Type: Bson
Default: None

The following code shows how to construct an DeleteOptions instance and pass it to the delete_one() method:

let opts: DeleteOptions = DeleteOptions::builder().comment(bson!("hello!")).build();
let res = my_coll.delete_one(filter, opts).await?;

The delete_one() and delete_many() methods return a DeleteResult type. This type contains the deleted_count property, which describes the number of documents deleted. If no documents match the query filter you specified, the delete operation does not remove any documents, and the value of deleted_count is 0.

This example shows how to call the delete_many() method with the following parameters:

  • A query filter that matches documents where the value of category is "garden"

  • A DeleteOptions instance that uses the _id_ index as the hint for the delete operation

let filter = doc! { "category": "garden" };
let hint = Hint::Name("_id_".to_string());
let opts: DeleteOptions = DeleteOptions::builder().hint(hint).build();
let res = my_coll.delete_many(filter, opts).await?;
println!("Deleted documents: {}", res.deleted_count);

Note

If you use the delete_one() method instead of delete_many() in the preceding code example, the driver deletes only the first of the two documents that match the query filter.

For runnable examples of the delete operations, see the following usage examples:

To learn more about the operations in this guide, see the following documentation:

To learn more about the methods and types mentioned in this guide, see the following API documentation:

← Modify Documents