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

..03-May-2022-

.evergreen/H08-Jan-2020-

benchmark/H08-Jan-2020-

bson/H08-Jan-2020-

cmd/H08-Jan-2020-

data/H08-Jan-2020-

etc/H08-Jan-2020-

event/H08-Jan-2020-

examples/documentation_examples/H08-Jan-2020-

internal/H08-Jan-2020-

mongo/H08-Jan-2020-

tag/H08-Jan-2020-

vendor/H08-Jan-2020-

version/H08-Jan-2020-

x/H08-Jan-2020-

.errcheck-excludesH A D08-Jan-2020849

.gitignoreH A D08-Jan-2020129

.gitmodulesH A D08-Jan-2020101

.lint-whitelistH A D08-Jan-20207.3 KiB

CONTRIBUTING.mdH A D08-Jan-20202.3 KiB

Gopkg.lockH A D08-Jan-202010.3 KiB

Gopkg.tomlH A D08-Jan-20201.1 KiB

LICENSEH A D08-Jan-202011.1 KiB

MakefileH A D08-Jan-20204.3 KiB

README.mdH A D08-Jan-20206.9 KiB

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