MongoDB Go客户端学习

同样,我们还是使用 docker 来快速创建一个 MongoDB 服务,用于开发使用:

1
2
3
4
5
6
7
8
9
10
11
12
version: '3'

services:
mongo:
container_name: mongo
image: mongo
network_mode: bridge
restart: on-failure
ports:
- "27017:27017"
volumes:
- "./data/mongo:/data/db"

insertOne

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
55
56
57
58
59
60
61
package main

import (
"context"
"fmt"
"github.com/mongodb/mongo-go-driver/bson/objectid"
"github.com/mongodb/mongo-go-driver/mongo"
"time"
)

type TimePoint struct {
StartTime int64 `bson:"startTime"`
EndTime int64 `bson:"endTime"`
}

type LogRecord struct {
JobName string `bson:"jobName"`
Command string `bson:"command"`
Err string `bson:"err"`
Content string `bson:"content"`
TimePoint TimePoint `bson:"timePoint"`
}

func main() {
var (
client *mongo.Client
err error
db *mongo.Database
coll *mongo.Collection
record *LogRecord
result *mongo.InsertOneResult
docId objectid.ObjectID
)
if client, err = mongo.Connect(context.TODO(), "mongodb://127.0.0.1:27017"); err != nil {
fmt.Println(err)
return
}
defer client.Disconnect(context.TODO())

db = client.Database("my_test_db")
coll = db.Collection("my_collection")

record = &LogRecord{
JobName: "hello",
Command: "echo hello",
Err: "",
Content: "hello",
TimePoint: TimePoint{
StartTime: time.Now().Unix(),
EndTime: time.Now().Unix() + 10,
},
}

if result, err = coll.InsertOne(context.TODO(), record); err != nil {
fmt.Println(err)
return
}

docId = result.InsertedID.(objectid.ObjectID)
fmt.Println(docId.Hex())
}

insertMany

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
55
56
57
58
59
60
61
62
63
64
65
66
67
package main

import (
"context"
"fmt"
"github.com/mongodb/mongo-go-driver/bson/objectid"
"github.com/mongodb/mongo-go-driver/mongo"
"time"
)

type TimePoint struct {
StartTime int64 `bson:"startTime"`
EndTime int64 `bson:"endTime"`
}

type LogRecord struct {
JobName string `bson:"jobName"`
Command string `bson:"command"`
Err string `bson:"err"`
Content string `bson:"content"`
TimePoint TimePoint `bson:"timePoint"`
}

func main() {
var (
client *mongo.Client
err error
db *mongo.Database
coll *mongo.Collection
record *LogRecord
result *mongo.InsertManyResult
docId objectid.ObjectID
logArray []interface{}
insertId interface{}
)
if client, err = mongo.Connect(context.TODO(), "mongodb://127.0.0.1:27017"); err != nil {
fmt.Println(err)
return
}
defer client.Disconnect(context.TODO())

db = client.Database("my_test_db")
coll = db.Collection("my_collection")

record = &LogRecord{
JobName: "hello",
Command: "echo hello",
Err: "",
Content: "hello",
TimePoint: TimePoint{
StartTime: time.Now().Unix(),
EndTime: time.Now().Unix() + 10,
},
}

logArray = []interface{}{record, record, record}

if result, err = coll.InsertMany(context.TODO(), logArray); err != nil {
fmt.Println(err)
return
}

for _, insertId = range result.InsertedIDs {
docId = insertId.(objectid.ObjectID)
fmt.Println(docId.Hex())
}
}

Find

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main

import (
"context"
"fmt"
"github.com/mongodb/mongo-go-driver/mongo"
"github.com/mongodb/mongo-go-driver/options"
)

type FindByJobName struct {
JobName string `bson:"jobName"`
}

type TimePoint struct {
StartTime int64 `bson:"startTime"`
EndTime int64 `bson:"endTime"`
}

type LogRecord struct {
JobName string `bson:"jobName"`
Command string `bson:"command"`
Err string `bson:"err"`
Content string `bson:"content"`
TimePoint TimePoint `bson:"timePoint"`
}

func main() {
var (
opt *options.ClientOptions
auth options.Credential
client *mongo.Client
err error
db *mongo.Database
coll *mongo.Collection
cond *FindByJobName
limit *int64
cursor mongo.Cursor
record *LogRecord
)
opt = &options.ClientOptions{}
auth = options.Credential{
AuthSource: "admin",
Username: "root",
Password: "root",
}
opt.SetAuth(auth)
if client, err = mongo.Connect(context.TODO(), "mongodb://127.0.0.1:27017", opt); err != nil {
fmt.Println(err)
return
}
defer client.Disconnect(context.TODO())

db = client.Database("my_test_db")
coll = db.Collection("my_collection")
cond = &FindByJobName{
JobName: "hello",
}
limit = new(int64)
*limit = 2
if cursor, err = coll.Find(context.TODO(), cond, &options.FindOptions{Limit: limit}); err != nil {
fmt.Println(err)
return
}
defer cursor.Close(context.TODO())

for cursor.Next(context.TODO()) {
record = &LogRecord{}
if err = cursor.Decode(record); err != nil {
fmt.Println(err)
return
}
fmt.Println(record)
}
}

Delete

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
55
56
57
package main

import (
"context"
"fmt"
"github.com/mongodb/mongo-go-driver/mongo"
"github.com/mongodb/mongo-go-driver/options"
"time"
)

type BeforeCond struct {
Before int64 `bson:"$lt"`
}

type DeleteCond struct {
BeforeCond BeforeCond `bson:"timePoint.startTime"`
}

func main() {
var (
opt *options.ClientOptions
auth options.Credential
client *mongo.Client
err error
db *mongo.Database
coll *mongo.Collection
delCond *DeleteCond
delResult *mongo.DeleteResult
)
opt = &options.ClientOptions{}
auth = options.Credential{
AuthSource: "admin",
Username: "root",
Password: "root",
}
opt.SetAuth(auth)
if client, err = mongo.Connect(context.TODO(), "mongodb://127.0.0.1:27017", opt); err != nil {
fmt.Println(err)
return
}
defer client.Disconnect(context.TODO())

db = client.Database("my_test_db")
coll = db.Collection("my_collection")

delCond = &DeleteCond{
BeforeCond: BeforeCond{
Before: time.Now().Unix(),
},
}
if delResult, err = coll.DeleteMany(context.TODO(), delCond); err != nil {
fmt.Println(err)
return
}

fmt.Println(delResult.DeletedCount)
}