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

..03-May-2022-

.evergreen/H03-May-2022-

benchmark/H02-Feb-2021-

bson/H02-Feb-2021-

cmd/H02-Feb-2021-

data/H02-Feb-2021-

etc/H02-Feb-2021-

event/H02-Feb-2021-

examples/documentation_examples/H02-Feb-2021-

internal/H02-Feb-2021-

mongo/H02-Feb-2021-

tag/H02-Feb-2021-

vendor/H03-May-2022-

version/H02-Feb-2021-

x/H02-Feb-2021-

.errcheck-excludesH A D02-Feb-2021849

.gitignoreH A D02-Feb-2021142

.gitmodulesH A D02-Feb-2021101

.lint-whitelistH A D02-Feb-20217.3 KiB

CONTRIBUTING.mdH A D02-Feb-20212.3 KiB

LICENSEH A D02-Feb-202111.1 KiB

MakefileH A D02-Feb-20214.7 KiB

README.mdH A D02-Feb-20217.6 KiB

THIRD-PARTY-NOTICESH A D02-Feb-202169.1 KiB

go.modH A D02-Feb-20211.1 KiB

go.sumH A D02-Feb-202113.7 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, create a `mongo.Client`:
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
57client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
58```
59
60And connect it to your running MongoDB server:
61
62```go
63ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
64defer cancel()
65err = client.Connect(ctx)
66```
67
68To do this in a single step, you can use the `Connect` function:
69
70```go
71ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
72defer cancel()
73client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
74```
75
76Make sure to defer a call to `Disconnect` after instantiating your client:
77
78```go
79defer func() {
80    if err = client.Disconnect(ctx); err != nil {
81        panic(err)
82    }
83}()
84```
85
86Calling `Connect` does not block for server discovery. If you wish to know if a MongoDB server has been found and connected to,
87use the `Ping` method:
88
89```go
90ctx, cancel = context.WithTimeout(context.Background(), 2*time.Second)
91defer cancel()
92err = client.Ping(ctx, readpref.Primary())
93```
94
95To insert a document into a collection, first retrieve a `Database` and then `Collection` instance from the `Client`:
96
97```go
98collection := client.Database("testing").Collection("numbers")
99```
100
101The `Collection` instance can then be used to insert documents:
102
103```go
104ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
105defer cancel()
106res, err := collection.InsertOne(ctx, bson.M{"name": "pi", "value": 3.14159})
107id := res.InsertedID
108```
109
110Several query methods return a cursor, which can be used like this:
111
112```go
113ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
114defer cancel()
115cur, err := collection.Find(ctx, bson.D{})
116if err != nil { log.Fatal(err) }
117defer cur.Close(ctx)
118for cur.Next(ctx) {
119   var result bson.M
120   err := cur.Decode(&result)
121   if err != nil { log.Fatal(err) }
122   // do something with result....
123}
124if err := cur.Err(); err != nil {
125  log.Fatal(err)
126}
127```
128
129For methods that return a single item, a `SingleResult` instance is returned:
130
131```go
132var result struct {
133    Value float64
134}
135filter := bson.M{"name": "pi"}
136ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
137defer cancel()
138err = collection.FindOne(ctx, filter).Decode(&result)
139if err != nil {
140    log.Fatal(err)
141}
142// Do something with result...
143```
144
145Additional examples and documentation can be found under the examples directory and [on the MongoDB Documentation website](https://docs.mongodb.com/drivers/go/).
146
147-------------------------
148## Bugs / Feature Reporting
149
150New Features and bugs can be reported on jira: https://jira.mongodb.org/browse/GODRIVER
151
152-------------------------
153## Testing / Development
154
155The 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.
156
157### Testing Different Topologies
158
159To test a **replica set** or **sharded cluster**, set `MONGODB_URI="<connection-string>"` for the `make` command.
160For example, for a local replica set named `rs1` comprised of three nodes on ports 27017, 27018, and 27019:
161
162```
163MONGODB_URI="mongodb://localhost:27017,localhost:27018,localhost:27018/?replicaSet=rs1" make
164```
165
166### Testing Auth and SSL
167
168To 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:
169
170```
171mongod \
172--auth \
173--sslMode requireSSL \
174--sslPEMKeyFile $(pwd)/data/certificates/server.pem \
175--sslCAFile $(pwd)/data/certificates/ca.pem \
176--sslWeakCertificateValidation
177```
178
179To 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:
180
181```
182AUTH=auth SSL=ssl MONGO_GO_DRIVER_CA_FILE=$(pwd)/data/certificates/ca.pem  MONGODB_URI="mongodb://user:password@localhost:27017/?authSource=admin" make
183```
184
185Notes:
186- The `--sslWeakCertificateValidation` flag is required on the server for the test suite to work correctly.
187- The test suite requires the auth database to be set with `?authSource=admin`, not `/admin`.
188
189### Testing Compression
190
191The 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:
192
193```
194MONGO_GO_DRIVER_COMPRESSOR=snappy make
195```
196
197Ensure the [`--networkMessageCompressors` flag](https://docs.mongodb.com/manual/reference/program/mongod/#cmdoption-mongod-networkmessagecompressors) on mongod or mongos includes `zlib` if testing zLib compression.
198
199-------------------------
200## Feedback
201
202The MongoDB Go Driver is not feature complete, so any help is appreciated. Check out the [project page](https://jira.mongodb.org/browse/GODRIVER)
203for tickets that need completing. See our [contribution guidelines](CONTRIBUTING.md) for details.
204
205-------------------------
206## Continuous Integration
207
208Commits to master are run automatically on [evergreen](https://evergreen.mongodb.com/waterfall/mongo-go-driver).
209
210-------------------------
211## Thanks and Acknowledgement
212
213<a href="https://github.com/ashleymcnamara">@ashleymcnamara</a> - Mongo Gopher Artwork
214
215-------------------------
216## License
217
218The MongoDB Go Driver is licensed under the [Apache License](LICENSE).
219