简体   繁体   中英

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

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

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.

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`
var res struct {
        Key   string `bson:"_id"`
        Value int
    }

Collection("kvmeta").FindOne(a.ctx, b.M{"_id": "version"}).Decode(&res)

version := res.Value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM