package main
import (
"context"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
// Set MongoDB connection parameters
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
// Connect to MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
// Ping the MongoDB server to check if the connection is successful
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
// Access the database
database := client.Database("your_database_name")
// List all collections (tables) in the database
collections, err := database.ListCollectionNames(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
// Remove each collection (table) from the database
for _, collection := range collections {
err := database.Collection(collection).Drop(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("Collection %s dropped successfully\n", collection)
}
// Disconnect from the MongoDB server
err = client.Disconnect(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println("Disconnected from MongoDB")
}