亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

使用 mongo go 驅動程序查找集合中的所有文檔

使用 mongo go 驅動程序查找集合中的所有文檔

Go
守候你守候我 2023-06-12 14:25:46
如何使用 mongo-go-driver 查找集合中的所有文檔?我嘗試通過nil過濾器,但這不會返回任何文檔,而是返回nil. 我還檢查了文件,但沒有看到任何關于歸還所有文件的提及。這是我嘗試過的上述結果。client, err := mongo.Connect(context.TODO(), "mongodb://localhost:27017")coll := client.Database("test").Collection("albums")if err != nil { fmt.Println(err) }// we can assume we're connected...right?fmt.Println("connected to mongodb")var results []*AlbumfindOptions := options.Find()cursor, err := coll.Find(context.TODO(), nil, findOptions)if err != nil {? ?fmt.Println(err) // prints 'document is nil'}findOptions另外,我對為什么我在調用集合上的函數時需要指定Find()(或者我不需要指定?)感到困惑。
查看完整描述

2 回答

?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

這是我使用 golang 的官方 MongoDB 驅動程序得出的結果。

//Find multiple documents

func FindRecords() {

? ? err := godotenv.Load()


? ? if err != nil {

? ? ? ? fmt.Println(err)

? ? }


? ? //Get database settings from env file

? ? //dbUser := os.Getenv("db_username")

? ? //dbPass := os.Getenv("db_pass")

? ? dbName := os.Getenv("db_name")

? ? docCollection := "retailMembers"


? ? dbHost := os.Getenv("db_host")

? ? dbPort := os.Getenv("db_port")

? ? dbEngine := os.Getenv("db_type")


? ? //set client options

? ? clientOptions := options.Client().ApplyURI("mongodb://" + dbHost + ":" + dbPort)

? ? //connect to MongoDB

? ? client, err := mongo.Connect(context.TODO(), clientOptions)

? ? if err != nil {

? ? ? ? log.Fatal(err)

? ? }


? ? //check the connection

? ? err = client.Ping(context.TODO(), nil)

? ? if err != nil {

? ? ? ? log.Fatal(err)

? ? }


? ? fmt.Println("Connected to " + dbEngine)

? ? db := client.Database(dbName).Collection(docCollection)


? ? //find records

? ? //pass these options to the Find method

? ? findOptions := options.Find()

? ? //Set the limit of the number of record to find

? ? findOptions.SetLimit(5)

? ? //Define an array in which you can store the decoded documents

? ? var results []Member


? ? //Passing the bson.D{{}} as the filter matches? documents in the collection

? ? cur, err := db.Find(context.TODO(), bson.D{{}}, findOptions)

? ? if err !=nil {

? ? ? ? log.Fatal(err)

? ? }

? ? //Finding multiple documents returns a cursor

? ? //Iterate through the cursor allows us to decode documents one at a time


? ? for cur.Next(context.TODO()) {

? ? ? ? //Create a value into which the single document can be decoded

? ? ? ? var elem Member

? ? ? ? err := cur.Decode(&elem)

? ? ? ? if err != nil {

? ? ? ? ? ? log.Fatal(err)

? ? ? ? }


? ? ? ? results =append(results, elem)


? ? }


? ? if err := cur.Err(); err != nil {

? ? ? ? log.Fatal(err)

? ? }


? ? //Close the cursor once finished

? ? cur.Close(context.TODO())


? ? fmt.Printf("Found multiple documents: %+v\n", results)



}


查看完整回答
反對 回復 2023-06-12
?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

嘗試傳遞一個空的bson.D而不是nil

cursor,?err?:=?coll.Find(context.TODO(),?bson.D{})

另外,FindOptions是可選的。

查看完整回答
反對 回復 2023-06-12
  • 2 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

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

公眾號

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