From 1f79bdf243b2efff743e6902b3e8ba3269f177e0 Mon Sep 17 00:00:00 2001 From: Tim Fogarty Date: Wed, 27 Feb 2019 12:03:10 -0800 Subject: [PATCH] Update for go-driver 1.0.0 --- Gopkg.lock | 46 +++++++++++++++++++++++----------------------- Gopkg.toml | 7 ++++--- README.md | 32 ++++++++++++++++++-------------- main.go | 11 +++++++---- 4 files changed, 52 insertions(+), 44 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index c9c3106..2d3a6b0 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -18,8 +18,24 @@ revision = "2e65f85255dbc3072edf28d6b5b8efc472979f5a" [[projects]] - digest = "1:820eeb72f78947f9b2e87bd99a57b5cc791c8fb47676a5c3824d408441ab1546" - name = "github.com/mongodb/mongo-go-driver" + branch = "master" + digest = "1:40fdfd6ab85ca32b6935853bbba35935dcb1d796c8135efd85947566c76e662e" + name = "github.com/xdg/scram" + packages = ["."] + pruneopts = "UT" + revision = "7eeb5667e42c09cb51bf7b7c28aea8c56767da90" + +[[projects]] + branch = "master" + digest = "1:f5c1d04bc09c644c592b45b9f0bad4030521b1a7d11c7dadbb272d9439fa6e8e" + name = "github.com/xdg/stringprep" + packages = ["."] + pruneopts = "UT" + revision = "73f8eece6fdcd902c185bf651de50f3828bed5ed" + +[[projects]] + digest = "1:7280a69811fe89d769f5cbbee7ec10cc33488d1e0d573c6d65e35a8887ab3420" + name = "go.mongodb.org/mongo-driver" packages = [ "bson", "bson/bsoncodec", @@ -53,24 +69,8 @@ "x/network/wiremessage", ] pruneopts = "UT" - revision = "29905d4bda472574c8b499e2f93b5b2747d8fbd7" - version = "v0.1.0" - -[[projects]] - branch = "master" - digest = "1:40fdfd6ab85ca32b6935853bbba35935dcb1d796c8135efd85947566c76e662e" - name = "github.com/xdg/scram" - packages = ["."] - pruneopts = "UT" - revision = "7eeb5667e42c09cb51bf7b7c28aea8c56767da90" - -[[projects]] - branch = "master" - digest = "1:f5c1d04bc09c644c592b45b9f0bad4030521b1a7d11c7dadbb272d9439fa6e8e" - name = "github.com/xdg/stringprep" - packages = ["."] - pruneopts = "UT" - revision = "73f8eece6fdcd902c185bf651de50f3828bed5ed" + revision = "ccf36d0607fa2f6f4ae9645e625a7f33b26cf6d1" + version = "v1.0.0-rc1" [[projects]] branch = "master" @@ -107,9 +107,9 @@ analyzer-name = "dep" analyzer-version = 1 input-imports = [ - "github.com/mongodb/mongo-go-driver/bson", - "github.com/mongodb/mongo-go-driver/mongo", - "github.com/mongodb/mongo-go-driver/mongo/options", + "go.mongodb.org/mongo-driver/bson", + "go.mongodb.org/mongo-driver/mongo", + "go.mongodb.org/mongo-driver/mongo/options", ] solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 56e4e49..e2acbc8 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -25,10 +25,11 @@ # unused-packages = true -[[constraint]] - name = "github.com/mongodb/mongo-go-driver" - version = "0.1.0" [prune] go-tests = true unused-packages = true + +[[constraint]] + name = "go.mongodb.org/mongo-driver" + version = "1.0.0-rc1" diff --git a/README.md b/README.md index 4e66858..7bd157b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# MongoDB Go Driver Tutorial +# MongoDB Go Driver Tutorial Part 1: Connecting, Using BSON, and CRUD Operations With the official MongoDB Go Driver [recently moving to beta](https://www.mongodb.com/blog/post/official-mongodb-go-driver-now-available-for-beta-testing), it's now regarded as feature complete and ready for a wider audience to start using. This tutorial will help you get started with the [MongoDB Go Driver](https://github.com/mongodb/mongo-go-driver/). You will create a simple program and learn how to: @@ -14,17 +14,17 @@ You can view the complete code for this tutorial on [this GitHub repository](htt The MongoDB Go Driver is made up of several packages. If you are just using `go get`, you can install the driver using: ``` -go get github.com/mongodb/mongo-go-driver +go get go.mongodb.org/mongo-driver ``` -The output of this may look like a warning stating something like `package github.com/mongodb/mongo-go-driver: no Go files in (...)`. This is expected output. +The output of this may look like a warning stating something like `package go.mongodb.org/mongo-driver: no Go files in (...)`. This is expected output. If you are using the [`dep`](https://golang.github.io/dep/docs/introduction.html) package manager, you can install the main `mongo` package as well as the `bson` and `mongo/options` package using this command: ``` -dep ensure --add github.com/mongodb/mongo-go-driver/mongo \ -github.com/mongodb/mongo-go-driver/bson \ -github.com/mongodb/mongo-go-driver/mongo/options +dep ensure --add go.mongodb.org/mongo-driver/mongo \ +go.mongodb.org/mongo-driver/bson \ +go.mongodb.org/mongo-driver/mongo/options ``` ## Create the wireframe @@ -39,9 +39,9 @@ import ( "fmt" "log" - "github.com/mongodb/mongo-go-driver/bson" - "github.com/mongodb/mongo-go-driver/mongo" - "github.com/mongodb/mongo-go-driver/mongo/options" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" ) // You will be using this Trainer type later in the program @@ -61,12 +61,16 @@ This code also imports some standard libraries and defines a `Trainer` type. You ## Connect to MongoDB using the Go Driver -Once the MongoDB Go Driver has been imported, you can connect to a MongoDB deployment using the `mongo.Connect()` function. You must pass a context and connection string to `mongo.Connect()`. Optionally, you can also pass in an `options.ClientOptions` object as a third argument to configure driver settings such as write concerns, socket timeouts, and more. [The options package documentation](https://godoc.org/github.com/mongodb/mongo-go-driver/mongo/options) has more information about what client options are available. +Once the MongoDB Go Driver has been imported, you can connect to a MongoDB deployment using the `mongo.Connect()` function. You must pass a context and a `options.ClientOptions` object to `mongo.Connect()`. The client options are used to set the connection string. It can also be used to configure driver settings such as write concerns, socket timeouts, and more. [The options package documentation](https://godoc.org/go.mongodb.org/mongo-driver/mongo/options) has more information about what client options are available. Add this code in the main function: ```go -client, err := mongo.Connect(context.TODO(), "mongodb://localhost:27017") +// Set client options +clientOptions := options.Client().ApplyURI("mongodb://localhost:27017") + +// Connect to MongoDB +client, err := mongo.Connect(context.TODO(), clientOptions) if err != nil { log.Fatal(err) @@ -126,7 +130,7 @@ bson.D{{ }} ``` -The `Raw` family of types is used for validating a slice of bytes. You can also retrieve single elements from Raw types using a [`Lookup()`](https://godoc.org/github.com/mongodb/mongo-go-driver/bson#Raw.Lookup). This is useful if you don't want the overhead of having to unmarshall the BSON into another type. This tutorial will just use the `D` family of types. +The `Raw` family of types is used for validating a slice of bytes. You can also retrieve single elements from Raw types using a [`Lookup()`](https://godoc.org/go.mongodb.org/mongo-driver/bson#Raw.Lookup). This is useful if you don't want the overhead of having to unmarshall the BSON into another type. This tutorial will just use the `D` family of types. ## CRUD Operations @@ -249,7 +253,7 @@ fmt.Printf("Found multiple documents (array of pointers): %+v\n", results) ### Delete Documents -Finally, you can delete documents using `collection.DeleteOne()` or `collection.DeleteMany()`. Here you pass `bson.D{{}}` as the filter argument, which will match all documents in the collection. You could also use [`collection.Drop()`](https://godoc.org/github.com/mongodb/mongo-go-driver/mongo#Collection.Drop) to delete an entire collection. +Finally, you can delete documents using `collection.DeleteOne()` or `collection.DeleteMany()`. Here you pass `bson.D{{}}` as the filter argument, which will match all documents in the collection. You could also use [`collection.Drop()`](https://godoc.org/go.mongodb.org/mongo-driver/mongo#Collection.Drop) to delete an entire collection. ```go deleteResult, err := collection.DeleteMany(context.TODO(), bson.D{{}}) @@ -261,6 +265,6 @@ fmt.Printf("Deleted %v documents in the trainers collection\n", deleteResult.Del ## Next steps -You can view the final code from this tutorial in [this GitHub repository](https://github.com/tfogo/mongodb-go-tutorial). Documentation for the MongoDB Go Driver is available on [GoDoc](https://godoc.org/github.com/mongodb/mongo-go-driver). You may be particularly interested in the documentation about using [aggregations](https://godoc.org/github.com/mongodb/mongo-go-driver/mongo#Collection.Aggregate) or [transactions](https://godoc.org/github.com/mongodb/mongo-go-driver/mongo#Session). +You can view the final code from this tutorial in [this GitHub repository](https://github.com/tfogo/mongodb-go-tutorial). Documentation for the MongoDB Go Driver is available on [GoDoc](https://godoc.org/github.com/mongodb/mongo-go-driver). You may be particularly interested in the documentation about using [aggregations](https://godoc.org/go.mongodb.org/mongo-driver/mongo#Collection.Aggregate) or [transactions](https://godoc.org/go.mongodb.org/mongo-driver/mongo#Session). If you have any questions, please get in touch in the [mongo-go-driver Google Group](https://groups.google.com/forum/#!forum/mongodb-go-driver). Please file any bug reports on the Go project in the [MongoDB JIRA](https://www.google.com/url?q=https%3A%2F%2Fjira.mongodb.org%2Fprojects%2FGODRIVER&sa=D&sntz=1&usg=AFQjCNEOEt6d3ZNOMKzmT23RYOVYdjSD6g). We would love your feedback on the Go Driver, so please get in touch with us to let us know your thoughts. diff --git a/main.go b/main.go index 88d22ec..d474693 100644 --- a/main.go +++ b/main.go @@ -5,9 +5,9 @@ import ( "fmt" "log" - "github.com/mongodb/mongo-go-driver/bson" - "github.com/mongodb/mongo-go-driver/mongo" - "github.com/mongodb/mongo-go-driver/mongo/options" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" ) type Trainer struct { @@ -18,8 +18,11 @@ type Trainer struct { func main() { + // Set client options + clientOptions := options.Client().ApplyURI("mongodb://localhost:27017") + // Connect to MongoDB - client, err := mongo.Connect(context.TODO(), "mongodb://localhost:27017") + client, err := mongo.Connect(context.TODO(), clientOptions) if err != nil { log.Fatal(err) }