Delete a Document
This version of the documentation is archived and no longer supported. View the current documentation to learn how to upgrade your version of the MongoDB Go Driver.
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"}} result, err := coll.DeleteOne(context.TODO(), filter) 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("573a13bff29313caabd5e06b"), ..., "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.