我需要使用CollectionNames()方法來列出數據庫的所有集合。我將不得不在 mongoDB中創建一個會話:sess := ... // obtain sessiondb := sess.DB("") // Get db, use db name if not given in connection urlnames, err := db.CollectionNames()我在哪里可以找到在 MongoDB 中獲取會話的示例?我一直與下一種方式的數據庫連接: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)我怎么能創建一個會話?提前致謝??!
1 回答

人到中年有點甜
TA貢獻1895條經驗 獲得超7個贊
您似乎混淆/混合了不同的 MongoDB 驅動程序。DB.CollectionNames()
存在于mgo
舊且未維護的驅動程序中。
您使用的驅動程序是官方mongo-go
驅動程序,它具有不同的方法來獲取現有集合列表。
mongo-go
驅動有Database.ListCollectionNames()
方法,你可以這樣使用:
names, err := mongo_cliente.ListCollectionNames(ctx, bson.D{}) // names is a []string holding all the existing collection names
- 1 回答
- 0 關注
- 103 瀏覽
添加回答
舉報
0/150
提交
取消