Docs Menu

Docs HomeGo

Delete a Document

On this page

  • Overview
  • Sample Data
  • Delete Operations
  • Parameters
  • Return Value
  • Additional Information
  • API Documentation

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

To run the example in this guide, load these documents into the tea.ratings collection with the following snippet:

coll := client.Database("tea").Collection("ratings")
docs := []interface{}{
bson.D{{"type", "Masala"}, {"rating", 10}},
bson.D{{"type", "Earl Grey"}, {"rating", 7}},
bson.D{{"type", "Oolong"}, {"rating", 10}},
bson.D{{"type", "Assam"}, {"rating", 7}},
}
result, err := coll.InsertMany(context.TODO(), docs)
if err != nil {
panic(err)
}
fmt.Printf("Number of documents inserted: %d\n", len(result.InsertedIDs))

Tip

Non-existent Databases and Collections

If the necessary database and collection don't exist when you perform a write operation, the server implicitly creates them.

Each document contains a rating for a type of tea, which corresponds to the type and rating fields.

Use delete operations to remove data from MongoDB. Delete operations consist of the following methods:

  • DeleteOne(), which deletes the first document that matches the filter

  • DeleteMany(), which deletes all documents that match the filter

Tip

If one document matches your filter when running the DeleteMany() method, it's equivalent to running the DeleteOne() method.

The DeleteOne() and DeleteMany() methods expect you to pass a Context type and a non-nil query filter specifying which documents to match.

They both optionally take a DeleteOptions type as a third parameter, which represents options you can use to configure the delete operation. If you don't specify a DeleteOptions, the driver uses the default values for each option.

The DeleteOptions type allows you to configure options with the following methods:

Method
Description
SetHint()
The index to use to scan for documents to delete.
Default: nil
SetCollation()
The type of language collation to use when sorting results.
Default: nil

The DeleteOne() and DeleteMany() methods return a DeleteResult type. This type contains the DeletedCount property, which states the number of documents deleted. If there are no matches to your filter, no document gets deleted and DeletedCount is 0.

The following example performs the following with the DeleteMany() method:

  • Matches and deletes documents where the rating is greater than 8

  • Specifies the method to use the _id as the index

filter := bson.D{{"rating", bson.D{{"$gt", 8}}}}
opts := options.Delete().SetHint(bson.D{{"_id", 1}})
result, err := coll.DeleteMany(context.TODO(), filter, opts)
if err != nil {
panic(err)
}
fmt.Printf("Number of documents deleted: %d\n", result.DeletedCount)

Tip

If the preceding example used the DeleteOne() method instead of DeleteMany(), the driver would delete the first of the two matched documents.

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

To learn more about performing the operations mentioned, see the following guides:

To learn about how the driver uses Context, see Context.

To learn more about any of the methods or types discussed in this guide, see the following API Documentation:

←  Insert a DocumentChange a Document →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.