第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用Golang庫獲取MongoDB版本?

如何使用Golang庫獲取MongoDB版本?

Go
皈依舞 2022-11-23 20:07:53
我正在使用 Go 的 MongodDB 驅動程序 ( https://pkg.go.dev/go.mongodb.org/mongo-driver@v1.8.0/mongo#section-documentation ) 并想獲取部署的 mongoDB 服務器的版本。例如,如果它是一個 MySQL 數據庫,我可以執(zhí)行如下操作: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 stringif err := db.QueryRow("SELECT VERSION()").Scan(&dbVersion); err != nil {    dbVersion = "NA"    log.Printf("Couldnt obtain db version: %w", err)}fmt.Println("DB Version: ", dbVersion)我瀏覽了文檔,但找不到線索。我還需要獲取其他元數據,例如特定數據庫的大小等。任何幫助,將不勝感激。謝謝!
查看完整描述

2 回答

?
呼如林

TA貢獻1798條經驗 獲得超3個贊

MongoDB 版本可以通過運行命令獲取,具體為buildInfocommand。


使用 shell,你可以這樣做:


db.runCommand({buildInfo: 1})

結果是一個文檔,其version屬性包含服務器版本,例如:


{

    "version" : "5.0.6",

    ...

}

要使用官方驅動程序運行命令,請使用Database.RunCommand()方法。


例如:


// Connect to MongoDB and acquire a Database:


ctx := context.Background()

opts := options.Client().ApplyURI("mongodb://localhost")

client, err := mongo.Connect(ctx, opts)

if err != nil {

    log.Fatalf("Failed to connect to db: %v", err)

}

defer client.Disconnect(ctx)


db := client.Database("your-db-name")


// And now run the buildInfo command:


buildInfoCmd := bson.D{bson.E{Key: "buildInfo", Value: 1}}

var buildInfoDoc bson.M

if err := db.RunCommand(ctx, buildInfoCmd).Decode(&buildInfoDoc); err != nil {

    log.Printf("Failed to run buildInfo command: %v", err)

    return

}

log.Println("Database version:", buildInfoDoc["version"])


查看完整回答
反對 回復 2022-11-23
?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

我們需要使用dbStats命令來獲取元數據。


host := "<your-host-name>:<pot-number>"

url := "mongodb://" + host



credential := options.Credential{

    AuthSource: "authentication-database",

    Username:   "username",

    Password:   "password",

}


clientOpts := options.Client().ApplyURI(url).SetAuth(credential)


ctx := context.Background()

client, err := mongo.Connect(ctx, clientOpts)

if err != nil {

    log.Fatal("Failed to connect to db : %w", err)

}

defer client.Disconnect(ctx)


if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {

    panic(err)

}

fmt.Println("Successfully connected and pinged.")


db := client.Database("your-database-name")



dbStatsCmd := bson.D{bson.E{Key: "dbStats", Value: 1}}

var dbStatsDoc bson.M

if err := db.RunCommand(ctx, dbStatsCmd).Decode(&dbStatsDoc); err != nil {

    log.Printf("Failed to run dbStats command: %v", err)

    return

}


log.Println("\nTotal Used Size in MB: ", dbStatsDoc["totalSize"].(float64) / 1048576 , " ,Total Free size in MB (part of total used size): ", dbStatsDoc["totalFreeStorageSize"].(float64)/1048576)



查看完整回答
反對 回復 2022-11-23
  • 2 回答
  • 0 關注
  • 242 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號