Created
April 7, 2022 09:57
-
-
Save jfjensen/3bcfb10503ee99f20fbde3cd9c263e56 to your computer and use it in GitHub Desktop.
This program will list all the databases present on the MongoDB server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"go.mongodb.org/mongo-driver/bson" | |
"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) | |
dbNames, err := client.ListDatabaseNames(ctx, bson.M{}) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(dbNames) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment