Delete a Document
On this page
Overview
In this guide, you can learn how to remove documents from your MongoDB collections using delete operations.
Sample Data
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.
Delete Operations
Use delete operations to remove data from MongoDB. Delete operations consist of the following methods:
DeleteOne()
, which deletes the first document that matches the filterDeleteMany()
, 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.
Parameters
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 |
Return Value
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
.
Example
The following example performs the following with the DeleteMany()
method:
Matches and deletes documents where the
rating
is greater than8
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.
Additional Information
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.
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API Documentation: