working on go version go1.10.2 linux/amd64. What am I missing? Tbh I don't quite understand all those context.Background() stuff, but I am going along with it.
v0.0.5-24-gcf7d3d6
[hendry@t480s lambda2mongo]$ cat main.go
package main
import (
"context"
)
func main() {
client, err := mongo.NewClient("mongodb://localhost:27017")
if err != nil {
log.WithError(err).Fatal("error connecting")
}
collection := client.Database("admin").Collection("system.version")
cur, err := collection.Find(context.Background(), nil)
if err != nil {
log.WithError(err).Fatal("failed to find collection")
}
defer cur.Close(context.Background())
for cur.Next(context.Background()) {
elem := bson.NewDocument()
err := cur.Decode(elem)
if err != nil {
log.WithError(err).Fatal("failed to decode element")
}
// do something with elem....
}
if err := cur.Err(); err != nil {
log.WithError(err).Fatal("failed to get cursor")
}
}
[hendry@t480s lambda2mongo]$ go run main.go
2018/05/28 15:32:18 fatal failed to find collection error=topology is closed
exit status 1
[hendry@t480s lambda2mongo]$ mongo mongodb://localhost:27017
MongoDB shell version v3.6.4
connecting to: mongodb://localhost:27017
MongoDB server version: 3.6.4
Server has startup warnings:
2018-05-28T14:37:21.072+0800 I STORAGE [initandlisten]
2018-05-28T14:37:21.072+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-05-28T14:37:21.580+0800 I CONTROL [initandlisten]
2018-05-28T14:37:21.580+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-05-28T14:37:21.580+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-05-28T14:37:21.580+0800 I CONTROL [initandlisten]
> use admin
switched to db admin
> db.system.version.find()
{ "_id" : "featureCompatibilityVersion", "version" : "3.6" }
>
bye
[hendry@t480s lambda2mongo]$