This chapter describes the Golang MongoDB document deletion operation.

Supported deletion operations

Collection.DeleteOne  - Delete a document based on a condition
Collection.DeleteMany  - Delete documents based on matching conditions

Delete a document

res, err := coll.DeleteOne(
						context.TODO(), // context parameter
						bson.D{{"name", "bob"}} // Set the query condition name=bob
					)
if err != nil {
	log.Fatal(err)
}
// Print the number of deleted documents
fmt.Printf("Successfully deleted %v documents\n", res.DeletedCount)

Delete a document based on the query condition.

Bulk document deletion

// Delete matching documents in bulk based on the query condition name=bob
res, err := coll.DeleteMany(context.TODO(), bson.D{{"name", "bob"}})
if err != nil {
		log.Fatal(err)
}
// Print the number of deleted documents
fmt.Printf("deleted %v documents\n", res.DeletedCount)