Docs Menu
Docs Home
/ / /
Go
/ /

Delete a Document

You can delete a document in a collection by using the DeleteOne() method.

Tip

Read the Usage Examples to learn how to run this example.

The following example matches documents in the movies collection in which the title is "Twilight", deleting the first document matched:

coll := client.Database("sample_mflix").Collection("movies")
filter := bson.D{{"title", "Twilight"}}
result, err := coll.DeleteOne(context.TODO(), filter)
if err != nil {
panic(err)
}

View a fully runnable example.

After you run the full example, it removes the following document in the movies collection:

// result truncated
{ "_id": ObjectId("573a13bff29313caabd5e06b"), ..., "title": "Twilight", ... }

For an example on how to find a document, see Find a Document.

To learn more about deleting documents, see Delete Documents.

DeleteOne()

Back

Replace a Document

Next

Delete Multiple Documents