|
macbookpro:go_dirver fredlee$ ll
total 16K
rw-rr- 1 fredlee staff 2.4K 2 27 16:19 Gopkg.lock
rw-rr- 1 fredlee staff 741 2 27 16:19 Gopkg.toml
rw-rr- 1 fredlee staff 3.7K 2 27 16:13 main.go
rw-rr- 1 fredlee staff 1.4K 2 27 14:57 sftp-config.json
drwxr-xr-x 5 fredlee staff 160 2 27 16:19 vendor
macbookpro:go_dirver fredlee$ dep ensure -add go.mongodb.org/mongo-driver/mongo
Fetching sources...
"go.mongodb.org/mongo-driver/mongo" is not imported by your project, and has been temporarily added to Gopkg.lock and vendor/.
If you run "dep ensure" again before actually importing it, it will disappear from Gopkg.lock and vendor/.
macbookpro:go_dirver fredlee$ ll
total 16K
rw-rr- 1 fredlee staff 2.4K 2 27 16:19 Gopkg.lock
rw-rr- 1 fredlee staff 741 2 27 16:19 Gopkg.toml
rw-rr- 1 fredlee staff 3.7K 2 27 16:13 main.go
rw-rr- 1 fredlee staff 1.4K 2 27 14:57 sftp-config.json
drwxr-xr-x 5 fredlee staff 160 2 27 16:19 vendor
macbookpro:go_dirver fredlee$ go build
go: creating new go.mod: module example/db/mongo/go_dirver
go: copying requirements from Gopkg.lock
go: finding github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc
go: extracting go.mongodb.org/mongo-driver v0.3.0
go: extracting github.com/mongodb/mongo-go-driver v0.3.0
go: downloading github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc
go: extracting github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc
- example/db/mongo/go_dirver
./main.go:101:45: "go.mongodb.org/mongo-driver/mongo/options".Client().SetAppName("myapp").SetConnectTimeout(time.Second).SetSocketTimeout(time.Second).SetServerSelectionTimeout(time.Second * 3).ApplyURI undefined (type *"go.mongodb.org/mongo-driver/mongo/options".ClientOptions has no field or method ApplyURI)
macbookpro:go_dirver fredlee$
func f3() {
clientOpts := options.Client().
SetAppName("myapp").
SetConnectTimeout(time.Second).
SetSocketTimeout(time.Second).
SetServerSelectionTimeout(time.Second * 3).
ApplyURI("mongodb://localhost:27017").
SetAuth(options.Credential
{
AuthMechanism: "SCRAM-SHA-1",
AuthSource: "tips",
Username: "xxx",
Password: "xxxx"}
)
client, err := mongo.NewClient(clientOpts)
checkErr(err)
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
checkErr(err)
collection := client.Database("tips").Collection("tips")
var tips []bson.M
ctx, _ = context.WithTimeout(context.Background(), 30*time.Second)
cur, err := collection.Find(ctx, bson.D{})
if err != nil
{
log.Fatal(err)
}
defer cur.Close(ctx)
for cur.Next(ctx) {
var result bson.M
err := cur.Decode(&result)
if err != nil {
log.Fatal(err)
}
// do something with result....
tips = append(tips, result)
}
if err := cur.Err(); err != nil {
log.Fatal(err)
}
}
|