Skip to content

Commit 91351a0

Browse files
committed
GODRIVER-1811 mongo/doc.go improvements (#563)
1 parent cc85d32 commit 91351a0

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

README.md

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ dep ensure -add "go.mongodb.org/mongo-driver/mongo"
4545
-------------------------
4646
## Usage
4747

48-
To get started with the driver, import the `mongo` package, create a `mongo.Client`:
48+
To get started with the driver, import the `mongo` package and create a `mongo.Client` with the `Connect` function:
4949

5050
```go
5151
import (
@@ -54,20 +54,6 @@ import (
5454
"go.mongodb.org/mongo-driver/mongo/readpref"
5555
)
5656

57-
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
58-
```
59-
60-
And connect it to your running MongoDB server:
61-
62-
```go
63-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
64-
defer cancel()
65-
err = client.Connect(ctx)
66-
```
67-
68-
To do this in a single step, you can use the `Connect` function:
69-
70-
```go
7157
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
7258
defer cancel()
7359
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
@@ -105,11 +91,11 @@ The `Collection` instance can then be used to insert documents:
10591
```go
10692
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
10793
defer cancel()
108-
res, err := collection.InsertOne(ctx, bson.M{"name": "pi", "value": 3.14159})
94+
res, err := collection.InsertOne(ctx, bson.D{{"name", "pi"}, {"value", 3.14159}})
10995
id := res.InsertedID
11096
```
11197

112-
To use `bson.M`, you will need to add `"go.mongodb.org/mongo-driver/bson"` to your imports.
98+
To use `bson.D`, you will need to add `"go.mongodb.org/mongo-driver/bson"` to your imports.
11399

114100
Your import statement should now look like this:
115101

@@ -131,7 +117,7 @@ cur, err := collection.Find(ctx, bson.D{})
131117
if err != nil { log.Fatal(err) }
132118
defer cur.Close(ctx)
133119
for cur.Next(ctx) {
134-
var result bson.M
120+
var result bson.D
135121
err := cur.Decode(&result)
136122
if err != nil { log.Fatal(err) }
137123
// do something with result....
@@ -147,7 +133,7 @@ For methods that return a single item, a `SingleResult` instance is returned:
147133
var result struct {
148134
Value float64
149135
}
150-
filter := bson.M{"name": "pi"}
136+
filter := bson.D{{"name", "pi"}}
151137
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
152138
defer cancel()
153139
err = collection.FindOne(ctx, filter).Decode(&result)
@@ -160,9 +146,11 @@ if err != nil {
160146
Additional examples and documentation can be found under the examples directory and [on the MongoDB Documentation website](https://docs.mongodb.com/drivers/go/).
161147

162148
-------------------------
163-
## Bugs / Feature Reporting
149+
## Feedback
150+
151+
For help with the driver, please post in the [MongoDB Community Forums](https://developer.mongodb.com/community/forums/tag/golang/).
164152

165-
New Features and bugs can be reported on jira: https://jira.mongodb.org/browse/GODRIVER
153+
New features and bugs can be reported on jira: https://jira.mongodb.org/browse/GODRIVER
166154

167155
-------------------------
168156
## Testing / Development
@@ -178,17 +166,17 @@ For example, for a local replica set named `rs1` comprised of three nodes on por
178166
MONGODB_URI="mongodb://localhost:27017,localhost:27018,localhost:27018/?replicaSet=rs1" make
179167
```
180168

181-
### Testing Auth and SSL
169+
### Testing Auth and TLS
182170

183-
To test authentication and SSL, first set up a MongoDB cluster with auth and SSL configured. Testing authentication requires a user with the `root` role on the `admin` database. The Go Driver repository comes with example certificates in the `data/certificates` directory. These certs can be used for testing. Here is an example command that would run a mongod with SSL correctly configured for tests:
171+
To test authentication and TLS, first set up a MongoDB cluster with auth and TLS configured. Testing authentication requires a user with the `root` role on the `admin` database. The Go Driver repository comes with example certificates in the `data/certificates` directory. These certs can be used for testing. Here is an example command that would run a mongod with TLS correctly configured for tests:
184172

185173
```
186174
mongod \
187175
--auth \
188-
--sslMode requireSSL \
189-
--sslPEMKeyFile $(pwd)/data/certificates/server.pem \
190-
--sslCAFile $(pwd)/data/certificates/ca.pem \
191-
--sslWeakCertificateValidation
176+
--tlsMode requireTLS \
177+
--tlsCertificateKeyFile $(pwd)/data/certificates/server.pem \
178+
--tlsCAFile $(pwd)/data/certificates/ca.pem \
179+
--tlsAllowInvalidCertificates
192180
```
193181

194182
To run the tests with `make`, set `MONGO_GO_DRIVER_CA_FILE` to the location of the CA file used by the database, set `MONGODB_URI` to the connection string of the server, set `AUTH=auth`, and set `SSL=ssl`. For example:
@@ -198,7 +186,7 @@ AUTH=auth SSL=ssl MONGO_GO_DRIVER_CA_FILE=$(pwd)/data/certificates/ca.pem MONGO
198186
```
199187

200188
Notes:
201-
- The `--sslWeakCertificateValidation` flag is required on the server for the test suite to work correctly.
189+
- The `--tlsAllowInvalidCertificates` flag is required on the server for the test suite to work correctly.
202190
- The test suite requires the auth database to be set with `?authSource=admin`, not `/admin`.
203191

204192
### Testing Compression
@@ -212,10 +200,9 @@ MONGO_GO_DRIVER_COMPRESSOR=snappy make
212200
Ensure the [`--networkMessageCompressors` flag](https://docs.mongodb.com/manual/reference/program/mongod/#cmdoption-mongod-networkmessagecompressors) on mongod or mongos includes `zlib` if testing zLib compression.
213201

214202
-------------------------
215-
## Feedback
203+
## Contribution
216204

217-
The MongoDB Go Driver is not feature complete, so any help is appreciated. Check out the [project page](https://jira.mongodb.org/browse/GODRIVER)
218-
for tickets that need completing. See our [contribution guidelines](CONTRIBUTING.md) for details.
205+
Check out the [project page](https://jira.mongodb.org/browse/GODRIVER) for tickets that need completing. See our [contribution guidelines](CONTRIBUTING.md) for details.
219206

220207
-------------------------
221208
## Continuous Integration

mongo/doc.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
// Package mongo provides a MongoDB Driver API for Go.
1010
//
1111
// Basic usage of the driver starts with creating a Client from a connection
12-
// string. To do so, call the NewClient and Connect functions:
12+
// string. To do so, call Connect:
1313
//
14-
// client, err := NewClient(options.Client().ApplyURI("mongodb://foo:bar@localhost:27017"))
15-
// if err != nil { return err }
16-
// ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
17-
// defer cancel()
18-
// err = client.Connect(ctx)
19-
// if err != nil { return err }
14+
// ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
15+
// defer cancel()
16+
// client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://foo:bar@localhost:27017"))
17+
// if err != nil { return err }
2018
//
2119
// This will create a new client and start monitoring the MongoDB server on localhost.
2220
// The Database and Collection types can be used to access the database:
@@ -52,6 +50,17 @@
5250
// return err
5351
// }
5452
//
53+
// Cursor.All will decode all of the returned elements at once:
54+
//
55+
// var results []struct{
56+
// Foo string
57+
// Bar int32
58+
// }
59+
// if err = cur.All(context.Background(), &results); err != nil {
60+
// log.Fatal(err)
61+
// }
62+
// // do something with results...
63+
//
5564
// Methods that only return a single document will return a *SingleResult, which works
5665
// like a *sql.Row:
5766
//

0 commit comments

Comments
 (0)