如何使用Golang库获取MongoDB版本?
我正在使用 Go 的 MongodDB 驱动程序 (https ://pkg.go.dev/go.mongodb.org/[email protected]/mongo#section-documentation)并希望获取部署的 mongoDB 服务器的版本。
例如,如果它是 MySQL 数据库,我可以执行如下操作:
db, err := sql.Open("mysql", DbUser+":"+DbPwd+"@tcp("+Host+")/"+DbName)
if err != nil {
log.Printf("Error while connecting to DB: %v", err)
}
defer db.Close()
var dbVersion string
if err := db.QueryRow("SELECT VERSION()").Scan(&dbVersion); err != nil {
dbVersion = "NA"
log.Printf("Couldnt obtain db version: %w", err)
}
fmt.Println("DB Version: ", dbVersion)
我浏览了文档,但找不到线索。
我还需要获取其他元数据,例如特定数据库的大小等。
任何帮助将不胜感激。谢谢!
I am using Go's MongodDB driver (https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#section-documentation) and want to obtain the version of the mongoDB server deployed.
For instance, if it would been a MySQL database, I can do something like below:
db, err := sql.Open("mysql", DbUser+":"+DbPwd+"@tcp("+Host+")/"+DbName)
if err != nil {
log.Printf("Error while connecting to DB: %v", err)
}
defer db.Close()
var dbVersion string
if err := db.QueryRow("SELECT VERSION()").Scan(&dbVersion); err != nil {
dbVersion = "NA"
log.Printf("Couldnt obtain db version: %w", err)
}
fmt.Println("DB Version: ", dbVersion)
I went through the documentation but am not able to find a clue.
I also need to fetch other metadata like Size of a particular database etc.
Any help would be appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MongoDB版本可以通过运行命令来获取,特别是
buildInfo
命令。使用 shell,您可以这样做:
结果是一个文档,其
version
属性保存服务器版本,例如:要使用官方驱动程序运行命令,请使用
Database.RunCommand()
方法。例如:
The MongoDB version can be acquired by running a command, specifically the
buildInfo
command.Using the shell, this is how you could do it:
The result is a document whose
version
property holds the server version, e.g.:To run commands using the official driver, use the
Database.RunCommand()
method.For example:
根据@icza的回答,以下是如何获取数据库的其他元数据:
我们需要使用
dbStats
命令获取元数据。Based on @icza's answer, here is how to obtain other metadata of the Database:
We need to use
dbStats
command to obtain metadata.