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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
package main
import (
"context"
"fmt"
"reflect"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type mongoClient struct {
dbName, colName string
client *mongo.Client
col *mongo.Collection
}
func (mc *mongoClient) init(dbName, colName string) error {
var err error
if err = mc.Connect(dbName, colName); err != nil {
return err
}
if err = mc.Drop(); err != nil {
return err
}
return nil
}
func (mc *mongoClient) Connect(dbName, colName string) error {
var err error
// 设置连接MongoDB的参数
clientOptions := options.Client().ApplyURI("mongodb://" + *mongoURI)
// 创建一个带有超时的上下文
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() // 确保在函数退出时取消上下文
// 使用带超时的上下文连接到MongoDB
mc.client, err = mongo.Connect(ctx, clientOptions)
if err != nil {
return err
}
// 尝试ping数据库以检查连接是否成功
err = mc.client.Ping(ctx, nil)
if err != nil {
return err
}
fmt.Println("Connected to MongoDB!")
mc.col = mc.client.Database(dbName).Collection(colName)
mc.dbName = dbName
mc.colName = colName
return nil
}
func (mc *mongoClient) InsertOne(document interface{}) error {
_, err := mc.col.InsertOne(context.Background(), document)
return err
}
func (mc *mongoClient) UpdateOne(filter, update interface{}) error {
_, err := mc.col.UpdateOne(context.Background(), filter, update)
return err
}
func (mc *mongoClient) UpdateMany(filter, update interface{}) error {
_, err := mc.col.UpdateMany(context.Background(), filter, update)
return err
}
func (mc *mongoClient) ReplaceOne(filter, new interface{}) error {
_, err := mc.col.ReplaceOne(context.Background(), filter, new)
return err
}
func (mc *mongoClient) Finddoc(filter bson.M, results interface{}) error {
sliceValue := reflect.ValueOf(results)
if sliceValue.Kind() != reflect.Ptr || sliceValue.Elem().Kind() != reflect.Slice {
return fmt.Errorf("Error: result argument must be pointer to slice")
}
cur, err := mc.col.Find(context.TODO(), filter)
if err != nil {
return err
}
defer cur.Close(context.TODO())
elemType := sliceValue.Elem().Type().Elem()
sliceValue = sliceValue.Elem()
for cur.Next(context.TODO()) {
elem := reflect.New(elemType).Interface()
err := cur.Decode(elem)
if err != nil {
return err
}
sliceValue = reflect.Append(sliceValue, reflect.ValueOf(elem).Elem())
}
reflect.ValueOf(results).Elem().Set(sliceValue)
return nil
}
func (mc *mongoClient) FindOneAndDelete(filter bson.M) (bson.M, error) {
res := mc.col.FindOneAndDelete(context.Background(), filter)
var result bson.M
err := res.Decode(&result)
return result, err
}
func (mc *mongoClient) Drop() error {
return mc.col.Drop(context.Background())
}
func (mc *mongoClient) Disconnect() error {
err := mc.client.Disconnect(context.Background())
if err != nil {
return err
}
mc.col = nil
mc.client = nil
mc.dbName = ""
mc.colName = ""
return nil
}
|