Last active
November 26, 2019 19:39
-
-
Save jerrymannel/b1df2c992a74f37036fb943ebe863ed7 to your computer and use it in GitHub Desktop.
Find the mongo server version and replica set information from golang client.
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" | |
"log" | |
"go.mongodb.org/mongo-driver/mongo" | |
"go.mongodb.org/mongo-driver/mongo/options" | |
"go.mongodb.org/mongo-driver/x/bsonx" | |
) | |
func main() { | |
url := "mongodb://localhost:27017" | |
clientOptions := options.Client().ApplyURI(url) | |
client, err := mongo.Connect(context.Background(), clientOptions) | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = client.Ping(context.Background(), nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("Connected to MongoDB!") | |
type MongoMetaData struct { | |
Version string `json:"version"` | |
Set string `json:"set"` | |
} | |
mongoMetaData := MongoMetaData{} | |
client.Database("test").RunCommand(nil, bsonx.Doc{{"buildInfo", bsonx.Int32(1)}}).Decode(&mongoMetaData) | |
fmt.Println(mongoMetaData.Version) | |
client.Database("admin").RunCommand(nil, bsonx.Doc{{"replSetGetStatus", bsonx.Int32(1)}}).Decode(&mongoMetaData) | |
fmt.Println(mongoMetaData.Set) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment