Docs Menu

Docs HomeGo

Delete Multiple Documents

You can delete multiple documents in a collection by using the DeleteMany() 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 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}}}}
results, err := coll.DeleteMany(context.TODO(), filter)
if err != nil {
panic(err)
}

View a fully runnable example.

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.

To learn more about deleting documents, see Delete a Document.

DeleteMany()

←  Delete a DocumentPerform Bulk Operations →
Share Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.