Docs Menu
Docs Home
/ / /
Go Driver

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:

// Deletes multiple documents from a collection by using the Go driver
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
// Defines a Restaurant struct as a model for documents in the "restaurants" collection
type Restaurant struct {
ID bson.ObjectID `bson:"_id"`
Name string `bson:"name"`
Borough string `bson:"borough"`
Cuisine string `bson:"cuisine"`
}
func main() {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
var uri string
if uri = os.Getenv("MONGODB_URI"); uri == "" {
log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable")
}
client, err := mongo.Connect(options.Client().ApplyURI(uri))
if err != nil {
panic(err)
}
defer func() {
if err = client.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()
coll := client.Database("sample_restaurants").Collection("restaurants")
filter := bson.D{
{"borough", "Queens"},
{"cuisine", "German"},
}
// Deletes all documents that have a "Borough" value of "Queens" and a "Cuisine" value of "German"
results, err := coll.DeleteMany(context.TODO(), filter)
if err != nil {
panic(err)
}
// Prints the number of deleted documents
fmt.Printf("Documents deleted: %d\n", results.DeletedCount)
// When you run this file for the first time, it prints output similar to the following:
// Documents deleted: 6
}

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 Documents.

DeleteMany()

Next

MongoDB Go Driver