Skip to content

Instantly share code, notes, and snippets.

@jfjensen
Created April 7, 2022 10:00
Show Gist options
  • Save jfjensen/7e80f1b097dee26d026f29c9b6e1aad0 to your computer and use it in GitHub Desktop.
Save jfjensen/7e80f1b097dee26d026f29c9b6e1aad0 to your computer and use it in GitHub Desktop.
This program will create a database collection on a MongoDB server
package main
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
ctx := context.TODO()
opts := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.Connect(ctx, opts)
if err != nil {
panic(err)
}
defer client.Disconnect(ctx)
fmt.Printf("%T\n", client)
testDB := client.Database("test")
fmt.Printf("%T\n", testDB)
exampleCollection := testDB.Collection("example")
defer exampleCollection.Drop(ctx)
fmt.Printf("%T\n", exampleCollection)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment