Skip to content

Commit 1f79bdf

Browse files
committed
Update for go-driver 1.0.0
1 parent bfa2f91 commit 1f79bdf

File tree

4 files changed

+52
-44
lines changed

4 files changed

+52
-44
lines changed

Gopkg.lock

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
# unused-packages = true
2626

2727

28-
[[constraint]]
29-
name = "github.com/mongodb/mongo-go-driver"
30-
version = "0.1.0"
3128

3229
[prune]
3330
go-tests = true
3431
unused-packages = true
32+
33+
[[constraint]]
34+
name = "go.mongodb.org/mongo-driver"
35+
version = "1.0.0-rc1"

README.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# MongoDB Go Driver Tutorial
1+
# MongoDB Go Driver Tutorial Part 1: Connecting, Using BSON, and CRUD Operations
22

33
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:
44

@@ -14,17 +14,17 @@ You can view the complete code for this tutorial on [this GitHub repository](htt
1414
The MongoDB Go Driver is made up of several packages. If you are just using `go get`, you can install the driver using:
1515

1616
```
17-
go get github.com/mongodb/mongo-go-driver
17+
go get go.mongodb.org/mongo-driver
1818
```
1919

20-
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.
20+
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.
2121

2222
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:
2323

2424
```
25-
dep ensure --add github.com/mongodb/mongo-go-driver/mongo \
26-
github.com/mongodb/mongo-go-driver/bson \
27-
github.com/mongodb/mongo-go-driver/mongo/options
25+
dep ensure --add go.mongodb.org/mongo-driver/mongo \
26+
go.mongodb.org/mongo-driver/bson \
27+
go.mongodb.org/mongo-driver/mongo/options
2828
```
2929

3030
## Create the wireframe
@@ -39,9 +39,9 @@ import (
3939
"fmt"
4040
"log"
4141

42-
"github.com/mongodb/mongo-go-driver/bson"
43-
"github.com/mongodb/mongo-go-driver/mongo"
44-
"github.com/mongodb/mongo-go-driver/mongo/options"
42+
"go.mongodb.org/mongo-driver/bson"
43+
"go.mongodb.org/mongo-driver/mongo"
44+
"go.mongodb.org/mongo-driver/mongo/options"
4545
)
4646

4747
// 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
6161

6262
## Connect to MongoDB using the Go Driver
6363

64-
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.
64+
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.
6565

6666
Add this code in the main function:
6767

6868
```go
69-
client, err := mongo.Connect(context.TODO(), "mongodb://localhost:27017")
69+
// Set client options
70+
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
71+
72+
// Connect to MongoDB
73+
client, err := mongo.Connect(context.TODO(), clientOptions)
7074

7175
if err != nil {
7276
log.Fatal(err)
@@ -126,7 +130,7 @@ bson.D{{
126130
}}
127131
```
128132

129-
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.
133+
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.
130134

131135
## CRUD Operations
132136

@@ -249,7 +253,7 @@ fmt.Printf("Found multiple documents (array of pointers): %+v\n", results)
249253

250254
### Delete Documents
251255

252-
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.
256+
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.
253257

254258
```go
255259
deleteResult, err := collection.DeleteMany(context.TODO(), bson.D{{}})
@@ -261,6 +265,6 @@ fmt.Printf("Deleted %v documents in the trainers collection\n", deleteResult.Del
261265

262266
## Next steps
263267

264-
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).
268+
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).
265269

266270
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.

main.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"fmt"
66
"log"
77

8-
"github.com/mongodb/mongo-go-driver/bson"
9-
"github.com/mongodb/mongo-go-driver/mongo"
10-
"github.com/mongodb/mongo-go-driver/mongo/options"
8+
"go.mongodb.org/mongo-driver/bson"
9+
"go.mongodb.org/mongo-driver/mongo"
10+
"go.mongodb.org/mongo-driver/mongo/options"
1111
)
1212

1313
type Trainer struct {
@@ -18,8 +18,11 @@ type Trainer struct {
1818

1919
func main() {
2020

21+
// Set client options
22+
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
23+
2124
// Connect to MongoDB
22-
client, err := mongo.Connect(context.TODO(), "mongodb://localhost:27017")
25+
client, err := mongo.Connect(context.TODO(), clientOptions)
2326
if err != nil {
2427
log.Fatal(err)
2528
}

0 commit comments

Comments
 (0)