I need to get the version of mongodb. The project is written in golang using mongo-go-driver.
How to get the version of mongodb by mongo-go-driver
1.3k Views Asked by xiongxiong At
2
There are 2 best solutions below
0

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.