11<p align =" center " ><img src =" etc/assets/mongo-gopher.png " width =" 250 " ></p >
22<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 >
3+ <a href =" https://goreportcard.com/report/go.mongodb.org/mongo-driver/v2 " ><img src =" https://goreportcard.com/badge/go.mongodb.org/mongo-driver/v2 " ></a >
4+ <a href =" https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/ mongo " ><img src =" etc/assets/godev-mongo-blue.svg " alt =" docs " ></a >
5+ <a href =" https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/ bson " ><img src =" etc/assets/godev-bson-blue.svg " alt =" docs " ></a >
66 <a href =" https://www.mongodb.com/docs/drivers/go/current/ " ><img src =" etc/assets/docs-mongodb-green.svg " ></a >
77</p >
88
99# MongoDB Go Driver
1010
1111The MongoDB supported driver for Go.
1212
13- ______________________________________________________________________
13+ See the following resources to learn more about upgrading from version 1.x to 2.0.:
14+
15+ - [ v2.0 Migration Guide] ( docs/migration-2.0.md )
16+ - [ v2.0 What's New] ( https://www.mongodb.com/docs/drivers/go/upcoming/whats-new/#what-s-new-in-2.0 )
1417
1518## Requirements
1619
1720- Go 1.18 or higher. We aim to support the latest versions of Go.
1821- Go 1.22 or higher is required to run the driver test suite.
1922- MongoDB 3.6 and higher.
2023
21- ______________________________________________________________________
22-
2324## Installation
2425
2526The recommended way to get started using the MongoDB Go driver is by using Go modules to install the dependency in
2627your project. This can be done either by importing packages from ` go.mongodb.org/mongo-driver ` and having the build
2728step install the dependency or by explicitly running
2829
2930``` bash
30- go get go.mongodb.org/mongo-driver/mongo
31+ go get go.mongodb.org/mongo-driver/v2/ mongo
3132```
3233
3334When using a version of Go that does not support modules, the driver can be installed using ` dep ` by running
3435
3536``` bash
36- dep ensure -add " go.mongodb.org/mongo-driver/mongo"
37+ dep ensure -add " go.mongodb.org/mongo-driver/v2/ mongo"
3738```
3839
39- ______________________________________________________________________
40-
4140## Usage
4241
4342To get started with the driver, import the ` mongo ` package and create a ` mongo.Client ` with the ` Connect ` function:
@@ -47,14 +46,12 @@ import (
4746 " context"
4847 " time"
4948
50- " go.mongodb.org/mongo-driver/mongo"
51- " go.mongodb.org/mongo-driver/mongo/options"
52- " go.mongodb.org/mongo-driver/mongo/readpref"
49+ " go.mongodb.org/mongo-driver/v2/ mongo"
50+ " go.mongodb.org/mongo-driver/v2/ mongo/options"
51+ " go.mongodb.org/mongo-driver/v2/ mongo/readpref"
5352)
5453
55- ctx , cancel := context.WithTimeout (context.Background (), 10 *time.Second )
56- defer cancel ()
57- client , err := mongo.Connect (options.Client ().ApplyURI (" mongodb://localhost:27017" ))
54+ client , _ := mongo.Connect (options.Client ().ApplyURI (" mongodb://localhost:27017" ))
5855```
5956
6057Make sure to defer a call to ` Disconnect ` after instantiating your client:
@@ -67,15 +64,16 @@ defer func() {
6764}()
6865```
6966
70- For more advanced configuration and authentication, see the [ documentation for mongo.Connect] ( https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Connect ) .
67+ For more advanced configuration and authentication, see the [ documentation for mongo.Connect] ( https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/ mongo#Connect ) .
7168
7269Calling ` Connect ` does not block for server discovery. If you wish to know if a MongoDB server has been found and connected to,
7370use the ` Ping ` method:
7471
7572``` go
7673ctx, cancel = context.WithTimeout (context.Background (), 2 *time.Second )
7774defer cancel ()
78- err = client.Ping (ctx, readpref.Primary ())
75+
76+ _ = client.Ping (ctx, readpref.Primary ())
7977```
8078
8179To insert a document into a collection, first retrieve a ` Database ` and then ` Collection ` instance from the ` Client ` :
@@ -89,11 +87,12 @@ The `Collection` instance can then be used to insert documents:
8987``` go
9088ctx, cancel = context.WithTimeout (context.Background (), 5 *time.Second )
9189defer cancel ()
92- res , err := collection.InsertOne (ctx, bson.D {{" name" , " pi" }, {" value" , 3.14159 }})
90+
91+ res , _ := collection.InsertOne (ctx, bson.D {{" name" , " pi" }, {" value" , 3.14159 }})
9392id := res.InsertedID
9493```
9594
96- To use ` bson.D ` , you will need to add ` "go.mongodb.org/mongo-driver/bson" ` to your imports.
95+ To use ` bson.D ` , you will need to add ` "go.mongodb.org/mongo-driver/v2/ bson" ` to your imports.
9796
9897Your import statement should now look like this:
9998
@@ -103,10 +102,10 @@ import (
103102 " log"
104103 " time"
105104
106- " go.mongodb.org/mongo-driver/bson"
107- " go.mongodb.org/mongo-driver/mongo"
108- " go.mongodb.org/mongo-driver/mongo/options"
109- " go.mongodb.org/mongo-driver/mongo/readpref"
105+ " go.mongodb.org/mongo-driver/v2/ bson"
106+ " go.mongodb.org/mongo-driver/v2/ mongo"
107+ " go.mongodb.org/mongo-driver/v2/ mongo/options"
108+ " go.mongodb.org/mongo-driver/v2/ mongo/readpref"
110109)
111110```
112111
@@ -115,15 +114,22 @@ Several query methods return a cursor, which can be used like this:
115114``` go
116115ctx, cancel = context.WithTimeout (context.Background (), 30 *time.Second )
117116defer cancel ()
117+
118118cur , err := collection.Find (ctx, bson.D {})
119- if err != nil { log.Fatal (err) }
119+ if err != nil {
120+ log.Fatal (err)
121+ }
122+
120123defer cur.Close (ctx)
121124for cur.Next (ctx) {
122125 var result bson.D
123- err := cur.Decode (&result)
124- if err != nil { log.Fatal (err) }
126+ if err := cur.Decode (&result); err != nil {
127+ log.Fatal (err)
128+ }
129+
125130 // do something with result....
126131}
132+
127133if err := cur.Err (); err != nil {
128134 log.Fatal (err)
129135}
@@ -135,16 +141,18 @@ For methods that return a single item, a `SingleResult` instance is returned:
135141var result struct {
136142 Value float64
137143}
144+
138145filter := bson.D {{" name" , " pi" }}
139146ctx, cancel = context.WithTimeout (context.Background (), 5 *time.Second )
140147defer cancel ()
148+
141149err = collection.FindOne (ctx, filter).Decode (&result)
142- if err == mongo.ErrNoDocuments {
150+ if errors. Is ( err, mongo.ErrNoDocuments ) {
143151 // Do something when no record was found
144- fmt.Println (" record does not exist" )
145152} else if err != nil {
146153 log.Fatal (err)
147154}
155+
148156// Do something with result...
149157```
150158
@@ -178,41 +186,29 @@ If compressors are set, the Go Driver negotiates with the server to select the f
178186
179187Messages compress when both parties enable network compression; otherwise, messages remain uncompressed
180188
181- ______________________________________________________________________
182-
183189## Feedback
184190
185191For help with the driver, please post in the [ MongoDB Community Forums] ( https://developer.mongodb.com/community/forums/tag/golang/ ) .
186192
187193New features and bugs can be reported on jira: https://jira.mongodb.org/browse/GODRIVER
188194
189- ______________________________________________________________________
190-
191195## Contribution
192196
193197Check out the [ project page] ( https://jira.mongodb.org/browse/GODRIVER ) for tickets that need completing. See our [ contribution guidelines] ( docs/CONTRIBUTING.md ) for details.
194198
195- ______________________________________________________________________
196-
197199## Continuous Integration
198200
199201Commits to master are run automatically on [ evergreen] ( https://evergreen.mongodb.com/waterfall/mongo-go-driver ) .
200202
201- ______________________________________________________________________
202-
203203## Frequently Encountered Issues
204204
205205See our [ common issues] ( docs/common-issues.md ) documentation for troubleshooting frequently encountered issues.
206206
207- ______________________________________________________________________
208-
209207## Thanks and Acknowledgement
210208
211209- The Go Gopher artwork by [ @ashleymcnamara ] ( https://github.com/ashleymcnamara )
212210- The original Go Gopher was designed by [ Renee French] ( http://reneefrench.blogspot.com/ )
213211
214- ______________________________________________________________________
215-
216212## License
217213
218214The MongoDB Go Driver is licensed under the [ Apache License] ( LICENSE ) .
0 commit comments