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

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

mongodb golang檢查集合存在

mongodb golang檢查集合存在

Go
森欄 2022-10-24 09:04:49
我需要檢查一個集合是否存在。我創(chuàng)建了以下功能:func ExitsCollection(name string) bool {    var exists bool = false    names, err := cliente.CollectionNames()    if err != nil {        log.Println("[-]I cannot retrieve the list of collections")    }    // Simply search in the names    for _, name := range names {        if name == name {            log.Printf("[+]The collection already exists!")            exists = true            break        }    }    if !exists {        log.Println("[+] The collection does not exist")    }    return exists}為了連接,我使用下一個功能:func ConectaBD() {    cliente_local, err := mongo.NewClient(options.Client().ApplyURI(cadena_conexion))    if err != nil {        log.Fatal(err)    }    ctx, cancelar = context.WithTimeout(context.Background(), 10*time.Second)    err = cliente_local.Connect(ctx)    if err != nil {        log.Fatal(err)    }    defer cancelar()    mongo_cliente = cliente_local.Database(DATABASE)    log.Println("[+]Connected to MongoDB Atlas")}我使用以下變量:var cliente_local *mongo.Clientvar mongo_cliente *mongo.Databasevar coleccion *mongo.Collectionvar ctx context.Contextvar cancelar context.CancelFunc問題是下一句話:名稱,錯誤:= cliente.CollectionNames()什么類型的數(shù)據(jù)或如何使用方法 CollectionNames()?有人有示例源代碼嗎?
查看完整描述

1 回答

?
慕蓋茨4494581

TA貢獻(xiàn)1850條經(jīng)驗 獲得超11個贊

Database.CollectionNames()返回 db 數(shù)據(jù)庫中存在的集合名稱。返回類型是slice這樣,您需要檢查您的收藏是否已列出。


請查看官方文檔:https ://pkg.go.dev/gopkg.in/mgo.v2#Database.CollectionNames


sess := ... // obtain session

db := sess.DB("") // Get db, use db name if not given in connection url


names, err := db.CollectionNames()

if err != nil {

    // Handle error

    log.Printf("Failed to get coll names: %v", err)

    return

}


// Simply search in the names slice, e.g.

for _, name := range names {

    if name == "collectionToCheck" {

        log.Printf("The collection exists!")

        break

    }

}


查看完整回答
反對 回復(fù) 2022-10-24
  • 1 回答
  • 0 關(guān)注
  • 241 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號