1- # mongo-go-driver
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/github.com/mongodb/mongo-go-driver " ><img src =" https://goreportcard.com/badge/github.com/mongodb/mongo-go-driver " ></a >
4+ <a href =" https://godoc.org/github.com/mongodb/mongo-go-driver/mongo " ><img src =" etc/assets/godoc-mongo-blue.svg " alt =" GoDoc " ></a >
5+ <a href =" https://godoc.org/github.com/mongodb/mongo-go-driver/bson " ><img src =" etc/assets/godoc-bson-blue.svg " alt =" GoDoc " ></a >
6+ <a href =" https://docs.mongodb.com/ecosystem/drivers/go.html " ><img src =" etc/assets/docs-mongodb-green.svg " ></a >
7+ </p >
28
3- MongoDB Driver for Go.
4- [ ![ GoDoc] ( https://godoc.org/github.com/mongodb/mongo-go-driver/mongo?status.svg )] ( https://godoc.org/github.com/mongodb/mongo-go-driver/mongo )
9+ # MongoDB Go Driver
10+
11+ The MongoDB supported driver for Go.
512
613-------------------------
714- [ Requirements] ( #requirements )
815- [ Installation] ( #installation )
16+ - [ Usage] ( #usage )
917- [ Bugs/Feature Reporting] ( #bugs-feature-reporting )
1018- [ Testing / Development] ( #testing--development )
1119- [ Continuous Integration] ( #continuous-integration )
@@ -14,7 +22,7 @@ MongoDB Driver for Go.
1422-------------------------
1523## Requirements
1624
17- - Go 1.9 or higher. We aim to support the latest supported versions go.
25+ - Go 1.10 or higher. We aim to support the latest supported versions of go.
1826- MongoDB 3.2 and higher.
1927
2028-------------------------
@@ -26,6 +34,88 @@ The recommended way to get started using the MongoDB Go driver is by using `dep`
2634dep ensure -add github.com/mongodb/mongo-go-driver/mongo
2735```
2836
37+ -------------------------
38+ ## Usage
39+
40+ To get started with the driver, import the ` mongo ` package, create a ` mongo.Client ` :
41+
42+ ``` go
43+ import " github.com/mongodb/mongo-go-driver/mongo"
44+
45+ client , err := mongo.NewClient (" mongodb://localhost:27017" )
46+ ```
47+
48+ And connect it to your running MongoDB server:
49+
50+ ``` go
51+ ctx , _ := context.WithTimeout (context.Background (), 10 *time.Second )
52+ err = client.Connect (ctx)
53+ ```
54+
55+ To do this in a single step, you can use the ` Connect ` function:
56+
57+ ``` go
58+ ctx , _ := context.WithTimeout (context.Background (), 10 *time.Second )
59+ client , err := mongo.Connect (ctx, " mongodb://localhost:27017" )
60+ ```
61+
62+ Calling ` Connect ` does not block for server discovery. If you wish to know if a MongoDB server has been found and connected to,
63+ use the ` Ping ` method:
64+
65+ ``` go
66+ ctx, _ = context.WithTimeout (context.Background (), 2 *time.Second )
67+ err = client.Ping (ctx, readpref.Primary ())
68+ ```
69+
70+ To insert a document into a collection, first retrieve a ` Database ` and then ` Collection ` instance from the ` Client ` :
71+
72+ ``` go
73+ collection := client.Database (" testing" ).Collection (" numbers" )
74+ ```
75+
76+ The ` Collection ` instance can then be used to insert documents:
77+
78+ ``` go
79+ ctx, _ = context.WithTimeout (context.Background (), 5 *time.Second )
80+ res , err := collection.InsertOne (ctx, bson.M {" name" : " pi" , " value" : 3.14159 })
81+ id := res.InsertedID
82+ ```
83+
84+ Several query methods return a cursor, which can be used like this:
85+
86+ ``` go
87+ ctx, _ = context.WithTimeout (context.Background (), 30 *time.Second )
88+ cur , err := collection.Find (ctx, nil )
89+ if err != nil { log.Fatal (err) }
90+ defer cur.Close (ctx)
91+ for cur.Next (ctx) {
92+ var result bson.M
93+ err := cur.Decode (&result)
94+ if err != nil { log.Fatal (err) }
95+ // do something with result....
96+ }
97+ if err := cur.Err (); err != nil {
98+ log.Fatal (err)
99+ }
100+ ```
101+
102+ For methods that return a single item, a ` SingleResult ` instance is returned:
103+
104+ ``` go
105+ var result struct {
106+ Value float64
107+ }
108+ filter := bson.M {" name" : " pi" }
109+ ctx, _ = context.WithTimeout (context.Background (), 5 *time.Second )
110+ err = collection.FindOne (ctx, filter).Decode (&result)
111+ if err != nil {
112+ log.Fatal (err)
113+ }
114+ // Do something with result...
115+ ```
116+
117+ Additional examples and documentation can be found under the examples directory and [ on the MongoDB Documentation website] ( https://docs.mongodb.com/ecosystem/drivers/go.html ) .
118+
29119-------------------------
30120## Bugs / Feature Reporting
31121
0 commit comments