Delete a Document
You can delete a document in a collection by using the DeleteOne()
method.
Example
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"}} // Deletes the first document that has a "title" value of "Twilight" result, err := coll.DeleteOne(context.TODO(), filter) // Prints a message if any errors occur during the operation if err != nil { panic(err) }
View a fully runnable example.
Expected Result
After you run the full example, it removes the following document
in the movies
collection:
// result truncated { "_id": ObjectId("..."), ..., "title": "Twilight", ... }
For an example on how to find a document, see Find a Document.
Additional Information
To learn more about deleting documents, see Delete Documents.