简体   繁体   English

如何通过mongo-go-driver获取mongodb的版本

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

I need to get the version of mongodb.我需要获取 mongodb 的版本。 The project is written in golang using mongo-go-driver.该项目是使用 mongo-go-driver 用 golang 编写的。

The version is returned as part of the buildInfo admin command.版本作为buildInfo管理命令的一部分返回。

Connect to the admin database and use the RunCommand function to run the buildInfo command.连接到 admin 数据库并使用RunCommand function 运行 buildInfo 命令。

The version field in the result will contain the MongoDB server version.结果中的version字段将包含 MongoDB 服务器版本。

The following code prints the version from the serverStatus database command.以下代码从serverStatus数据库命令打印版本。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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