Docs Menu

Docs HomeGo

Delete Documents

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.

The examples in this guide use the following Book struct as a model for documents in the books collection:

type Book struct {
Title string
Author string
Length int32
}

To run the examples in this guide, load the sample data into the db.books collection with the following snippet:

coll := client.Database("db").Collection("books")
docs := []interface{}{
Book{Title: "Atonement", Author: "Ian McEwan", Length: 351},
Book{Title: "My Brilliant Friend", Author: "Elena Ferrante", Length: 331},
Book{Title: "Lucy", Author: "Jamaica Kincaid", Length: 103},
Book{Title: "Outline", Author: "Rachel Cusk", Length: 258},
}
result, err := coll.InsertMany(context.TODO(), docs)

Each document contains a description of a book that includes the title, author, and page length corresponding to the title, author, and length fields in each document.

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.

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 length is greater than 300

  • Instructs the method to use the _id as the index

filter := bson.D{{"length", bson.D{{"$gt", 300}}}}
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 specifying hints, see Indexes.

To learn more about collations, see Collations.

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

←  Insert a DocumentModify Documents →