• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.evergreen/H03-May-2022-

benchmark/H30-Mar-2021-

bson/H30-Mar-2021-

cmd/H30-Mar-2021-

data/H30-Mar-2021-

etc/H30-Mar-2021-

event/H30-Mar-2021-

examples/documentation_examples/H30-Mar-2021-

internal/H30-Mar-2021-

mongo/H30-Mar-2021-

tag/H30-Mar-2021-

vendor/H03-May-2022-

version/H30-Mar-2021-

x/H30-Mar-2021-

.errcheck-excludesH A D30-Mar-2021849

.gitignoreH A D30-Mar-2021142

.gitmodulesH A D30-Mar-2021101

.lint-whitelistH A D30-Mar-20217.3 KiB

CONTRIBUTING.mdH A D30-Mar-20212.3 KiB

LICENSEH A D30-Mar-202111.1 KiB

MakefileH A D30-Mar-20215.1 KiB

README.mdH A D30-Mar-20217.8 KiB

THIRD-PARTY-NOTICESH A D30-Mar-202179.2 KiB

go.modH A D30-Mar-20211 KiB

go.sumH A D30-Mar-202114 KiB

README.md

1<p align="center"><img src="etc/assets/mongo-gopher.png" width="250"></p>
2<p align="center">
3  <a href="https://goreportcard.com/report/go.mongodb.org/mongo-driver"><img src="https://goreportcard.com/badge/go.mongodb.org/mongo-driver"></a>
4  <a href="https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo"><img src="etc/assets/godev-mongo-blue.svg" alt="docs"></a>
5  <a href="https://pkg.go.dev/go.mongodb.org/mongo-driver/bson"><img src="etc/assets/godev-bson-blue.svg" alt="docs"></a>
6  <a href="https://docs.mongodb.com/drivers/go/"><img src="etc/assets/docs-mongodb-green.svg"></a>
7</p>
8
9# MongoDB Go Driver
10
11The MongoDB supported driver for Go.
12
13-------------------------
14- [Requirements](#requirements)
15- [Installation](#installation)
16- [Usage](#usage)
17- [Bugs / Feature Reporting](#bugs--feature-reporting)
18- [Testing / Development](#testing--development)
19- [Continuous Integration](#continuous-integration)
20- [License](#license)
21
22-------------------------
23## Requirements
24
25- Go 1.10 or higher. We aim to support the latest supported versions of go.
26- MongoDB 2.6 and higher.
27
28-------------------------
29## Installation
30
31The recommended way to get started using the MongoDB Go driver is by using go modules to install the dependency in
32your project. This can be done either by importing packages from `go.mongodb.org/mongo-driver` and having the build
33step install the dependency or by explicitly running
34
35```bash
36go get go.mongodb.org/mongo-driver/mongo
37```
38
39When using a version of Go that does not support modules, the driver can be installed using `dep` by running
40
41```bash
42dep ensure -add "go.mongodb.org/mongo-driver/mongo"
43```
44
45-------------------------
46## Usage
47
48To get started with the driver, import the `mongo` package and create a `mongo.Client` with the `Connect` function:
49
50```go
51import (
52    "go.mongodb.org/mongo-driver/mongo"
53    "go.mongodb.org/mongo-driver/mongo/options"
54    "go.mongodb.org/mongo-driver/mongo/readpref"
55)
56
57ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
58defer cancel()
59client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
60```
61
62Make sure to defer a call to `Disconnect` after instantiating your client:
63
64```go
65defer func() {
66    if err = client.Disconnect(ctx); err != nil {
67        panic(err)
68    }
69}()
70```
71
72For more advanced configuration and authentication, see the [documentation for mongo.Connect](https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Connect).
73
74Calling `Connect` does not block for server discovery. If you wish to know if a MongoDB server has been found and connected to,
75use the `Ping` method:
76
77```go
78ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)
79defer cancel()
80err = client.Ping(ctx, readpref.Primary())
81```
82
83To insert a document into a collection, first retrieve a `Database` and then `Collection` instance from the `Client`:
84
85```go
86collection := client.Database("testing").Collection("numbers")
87```
88
89The `Collection` instance can then be used to insert documents:
90
91```go
92ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
93defer cancel()
94res, err := collection.InsertOne(ctx, bson.D{{"name", "pi"}, {"value", 3.14159}})
95id := res.InsertedID
96```
97
98To use `bson.D`, you will need to add `"go.mongodb.org/mongo-driver/bson"` to your imports.
99
100Your import statement should now look like this:
101
102```go
103import (
104    "go.mongodb.org/mongo-driver/bson"
105    "go.mongodb.org/mongo-driver/mongo"
106    "go.mongodb.org/mongo-driver/mongo/options"
107    "go.mongodb.org/mongo-driver/mongo/readpref"
108)
109```
110
111Several query methods return a cursor, which can be used like this:
112
113```go
114ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
115defer cancel()
116cur, err := collection.Find(ctx, bson.D{})
117if err != nil { log.Fatal(err) }
118defer cur.Close(ctx)
119for cur.Next(ctx) {
120   var result bson.D
121   err := cur.Decode(&result)
122   if err != nil { log.Fatal(err) }
123   // do something with result....
124}
125if err := cur.Err(); err != nil {
126  log.Fatal(err)
127}
128```
129
130For methods that return a single item, a `SingleResult` instance is returned:
131
132```go
133var result struct {
134    Value float64
135}
136filter := bson.D{{"name", "pi"}}
137ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
138defer cancel()
139err = collection.FindOne(ctx, filter).Decode(&result)
140if err != nil {
141    log.Fatal(err)
142}
143// Do something with result...
144```
145
146Additional examples and documentation can be found under the examples directory and [on the MongoDB Documentation website](https://docs.mongodb.com/drivers/go/).
147
148-------------------------
149## Feedback
150
151For help with the driver, please post in the [MongoDB Community Forums](https://developer.mongodb.com/community/forums/tag/golang/).
152
153New features and bugs can be reported on jira: https://jira.mongodb.org/browse/GODRIVER
154
155-------------------------
156## Testing / Development
157
158The driver tests can be run against several database configurations. The most simple configuration is a standalone mongod with no auth, no ssl, and no compression. To run these basic driver tests, make sure a standalone MongoDB server instance is running at localhost:27017. To run the tests, you can run `make` (on Windows, run `nmake`). This will run coverage, run go-lint, run go-vet, and build the examples.
159
160### Testing Different Topologies
161
162To test a **replica set** or **sharded cluster**, set `MONGODB_URI="<connection-string>"` for the `make` command.
163For example, for a local replica set named `rs1` comprised of three nodes on ports 27017, 27018, and 27019:
164
165```
166MONGODB_URI="mongodb://localhost:27017,localhost:27018,localhost:27018/?replicaSet=rs1" make
167```
168
169### Testing Auth and TLS
170
171To 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:
172
173```
174mongod \
175--auth \
176--tlsMode requireTLS \
177--tlsCertificateKeyFile $(pwd)/data/certificates/server.pem \
178--tlsCAFile $(pwd)/data/certificates/ca.pem \
179--tlsAllowInvalidCertificates
180```
181
182To 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:
183
184```
185AUTH=auth SSL=ssl MONGO_GO_DRIVER_CA_FILE=$(pwd)/data/certificates/ca.pem  MONGODB_URI="mongodb://user:password@localhost:27017/?authSource=admin" make
186```
187
188Notes:
189- The `--tlsAllowInvalidCertificates` flag is required on the server for the test suite to work correctly.
190- The test suite requires the auth database to be set with `?authSource=admin`, not `/admin`.
191
192### Testing Compression
193
194The MongoDB Go Driver supports wire protocol compression using Snappy, zLib, or zstd. To run tests with wire protocol compression, set `MONGO_GO_DRIVER_COMPRESSOR` to `snappy`, `zlib`, or `zstd`.  For example:
195
196```
197MONGO_GO_DRIVER_COMPRESSOR=snappy make
198```
199
200Ensure the [`--networkMessageCompressors` flag](https://docs.mongodb.com/manual/reference/program/mongod/#cmdoption-mongod-networkmessagecompressors) on mongod or mongos includes `zlib` if testing zLib compression.
201
202-------------------------
203## Contribution
204
205Check out the [project page](https://jira.mongodb.org/browse/GODRIVER) for tickets that need completing. See our [contribution guidelines](CONTRIBUTING.md) for details.
206
207-------------------------
208## Continuous Integration
209
210Commits to master are run automatically on [evergreen](https://evergreen.mongodb.com/waterfall/mongo-go-driver).
211
212-------------------------
213## Thanks and Acknowledgement
214
215<a href="https://github.com/ashleymcnamara">@ashleymcnamara</a> - Mongo Gopher Artwork
216
217-------------------------
218## License
219
220The MongoDB Go Driver is licensed under the [Apache License](LICENSE).
221