Find all documents in a collection with mongo go driver

Kelly Flet :

I checked out the answer here but this uses the old and unmaintained mgo. How can I find all documents in a collection using the mongo-go-driver?

I tried passing a nil filter, but this does not return any documents and instead returns nil. I also checked the documentation but did not see any mention of returning all documents. Here is what I've tried with aforementioned result.

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 []*Album
findOptions := options.Find()
cursor, err := coll.Find(context.TODO(), nil, findOptions)
if err != nil {
   fmt.Println(err) // prints 'document is nil'
}

Also, I'm about confused about why I need to specify findOptions when I've called the Find() function on the collection (or do I not need to specify?).

Moshe Revah :

Try passing an empty bson.D instead of nil:

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

Also, FindOptions is optional.

Disclaimer: I've never used the official driver, but there are a few examples at https://godoc.org/go.mongodb.org/mongo-driver/mongo

Seems like their tutorial is outdated :/

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Retrieving all documents without iterating the cursor using mongo driver

分類Dev

Get all documents from mongo collection using a nested list comprehension in Python

分類Dev

Get all documents from mongo collection using a nested list comprehension in Python

分類Dev

Get All documents in a Collection CosmosDb

分類Dev

go mongo-driverライブラリでFind()。Select()。One()を使用する方法

分類Dev

Get All 'documents' from MongoDB 'collection'

分類Dev

How to get all documents from a collection in FaunaDB?

分類Dev

mongo-go-driver logger&log level config

分類Dev

convert result into JSON without structs using mongo-go-driver

分類Dev

Mongo-go-driver GridFSメタデータ

分類Dev

How to unset a field from all documents with php mongo adapter?

分類Dev

Ignore all fields (keys) into Mongo find

分類Dev

Updating all documents in a collection (million+) with Date object

分類Dev

What is the preferred way to add many fields to all documents in a MongoDB collection?

分類Dev

Mongo DB, count documents

分類Dev

mongodb/mongoose findMany - find all documents with IDs listed in array

分類Dev

How to find all documents where the value of one field matches that of another

分類Dev

Find documents where ALL elements of an array have a specific value

分類Dev

mongo-go-driverでmongoコマンドを実行する方法は?

分類Dev

mongo-go-driverで.FindOneのフィルターを作成する

分類Dev

公式のmongo-go-driverとのMongodb unixソケット接続?

分類Dev

mongo-go-driverコンテキストラッパー関数

分類Dev

mongo-go-driverでカーソルをモックする方法

分類Dev

大なり演算子を使用したGolang mongo-go-driver Beta 1

分類Dev

How to use the elements of a slice in bson.A using mongo-go-driver 0.2.0

分類Dev

goのmongo-driver bsonをインポートする方法

分類Dev

Dockerとmongo-go-driverの「サーバー選択エラー」

分類Dev

接続に失敗するMongo-Go-Driver

分類Dev

How to find all the driver packages installed in my pc?

Related 関連記事

  1. 1

    Retrieving all documents without iterating the cursor using mongo driver

  2. 2

    Get all documents from mongo collection using a nested list comprehension in Python

  3. 3

    Get all documents from mongo collection using a nested list comprehension in Python

  4. 4

    Get All documents in a Collection CosmosDb

  5. 5

    go mongo-driverライブラリでFind()。Select()。One()を使用する方法

  6. 6

    Get All 'documents' from MongoDB 'collection'

  7. 7

    How to get all documents from a collection in FaunaDB?

  8. 8

    mongo-go-driver logger&log level config

  9. 9

    convert result into JSON without structs using mongo-go-driver

  10. 10

    Mongo-go-driver GridFSメタデータ

  11. 11

    How to unset a field from all documents with php mongo adapter?

  12. 12

    Ignore all fields (keys) into Mongo find

  13. 13

    Updating all documents in a collection (million+) with Date object

  14. 14

    What is the preferred way to add many fields to all documents in a MongoDB collection?

  15. 15

    Mongo DB, count documents

  16. 16

    mongodb/mongoose findMany - find all documents with IDs listed in array

  17. 17

    How to find all documents where the value of one field matches that of another

  18. 18

    Find documents where ALL elements of an array have a specific value

  19. 19

    mongo-go-driverでmongoコマンドを実行する方法は?

  20. 20

    mongo-go-driverで.FindOneのフィルターを作成する

  21. 21

    公式のmongo-go-driverとのMongodb unixソケット接続?

  22. 22

    mongo-go-driverコンテキストラッパー関数

  23. 23

    mongo-go-driverでカーソルをモックする方法

  24. 24

    大なり演算子を使用したGolang mongo-go-driver Beta 1

  25. 25

    How to use the elements of a slice in bson.A using mongo-go-driver 0.2.0

  26. 26

    goのmongo-driver bsonをインポートする方法

  27. 27

    Dockerとmongo-go-driverの「サーバー選択エラー」

  28. 28

    接続に失敗するMongo-Go-Driver

  29. 29

    How to find all the driver packages installed in my pc?

ホットタグ

アーカイブ