-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
54 lines (49 loc) · 1.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
c := context.Background()
mc, err := mongo.Connect(c, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
panic(err)
}
col := mc.Database("awesomeTest").Collection("test")
insertRows(c, col)
}
func findRows(c context.Context, col *mongo.Collection) {
cur, err := col.Find(c, bson.M{})
if err != nil {
panic(err)
}
for cur.Next(c) {
var row struct {
ID primitive.ObjectID `bson:"_id"`
OpenID string `bson:"open_id"`
}
err = cur.Decode(&row)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", row)
}
}
func insertRows(c context.Context, col *mongo.Collection) {
res, err := col.InsertMany(c, []interface{}{
bson.M{
"test_id": "123",
},
bson.M{
"test_id": "456",
},
})
if err != nil {
panic(err)
}
fmt.Printf("%+v", res)
}