Delete Multiple Documents
You can delete multiple documents in a collection by using the
DeleteMany()
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 runtime
is greater than 800 minutes, deleting all
documents matched:
coll := client.Database("sample_mflix").Collection("movies") filter := bson.D{{"runtime", bson.D{{"$gt", 800}}}} // Deletes all documents that have a "runtime" value greater than 800 results, err := coll.DeleteMany(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 documents
in the movies
collection:
// results truncated { "_id": ObjectId("573a1397f29313caabce69db"), ... , "runtime": 1256, ... }, { "_id": ObjectId("573a1397f29313caabce75fe"), ... , "runtime": 910, ... }, { "_id": ObjectId("573a1399f29313caabcee1aa"), ... , "runtime": 1140, ... }, { "_id": ObjectId("573a13a6f29313caabd18ae0"), ... , "runtime": 877, ... }
For an example on how to find multiple documents, see Find Multiple Documents.
Additional Information
To learn more about deleting documents, see Delete Documents.