How to get the version of mongodb by mongo-go-driver

Viewed 667

I need to get the version of mongodb. The project is written in golang using mongo-go-driver.

2 Answers

The following code prints the version from the serverStatus database command.

var commandResult bson.M
command := bson.D{{ "serverStatus", 1 }}
err := client.Database("test").RunCommand(context.TODO(), command).Decode(&commandResult);

if err != nil {
    log.Fatal(err)
}       

fmt.Printf("Db version: %+v\n", commandResult["version"]) // e.g., `4.2.8`

The version is returned as part of the buildInfo admin command.

Connect to the admin database and use the RunCommand function to run the buildInfo command.

The version field in the result will contain the MongoDB server version.

Related