This repository has been archived by the owner on Jan 16, 2020. It is now read-only.
/
mongo.go
230 lines (211 loc) · 5.77 KB
/
mongo.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package mongo
import (
"context"
"fmt"
"github.com/xd-meal-back-end/pkg/logging"
"github.com/xd-meal-back-end/pkg/setting"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/x/bsonx"
"log"
"strings"
"time"
)
type MongoUtils struct {
Con *mongo.Client
Db *mongo.Database
}
func (o *MongoUtils) OpenConn() (con *mongo.Client) {
uri := fmt.Sprintf("mongodb://%s:%s@%s:%d", setting.MongoSetting.User, setting.MongoSetting.Password, setting.MongoSetting.Host,
setting.MongoSetting.Port)
// Initialize and Connect the mongo client to the MongoDB server
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
con, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
// Ping MongoDB
ctx, _ = context.WithTimeout(context.Background(), 2*time.Second)
if err = con.Ping(ctx, readpref.Primary()); err != nil {
log.Fatalf("mongo.Setup err: %v", err)
return
}
//fmt.Println("connected to nosql database:", uri)
o.Con = con
return con
}
func Setup() {
m := MongoUtils{}
(&m).OpenConn()
}
func (o *MongoUtils) SetDb(db string) {
if o.Con == nil {
panic("连接为空...")
}
o.Db = o.Con.Database(db)
}
func (o *MongoUtils) UpdateAll(col string, filter bson.M, update bson.M) (int64, error) {
if o.Db == nil || o.Con == nil {
return 0, fmt.Errorf("没有初始化连接和数据库信息!")
}
table := o.Db.Collection(col)
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
result, err := table.UpdateMany(ctx, filter, update)
//fmt.Println(result)
if err != nil {
return 0, err
}
return result.ModifiedCount, nil
}
func (o *MongoUtils) FindOne(col string, filter bson.M) (bson.M, error) {
if o.Db == nil || o.Con == nil {
return nil, fmt.Errorf("没有初始化连接和数据库信息!")
}
table := o.Db.Collection(col)
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
var result bson.M
err := table.FindOne(ctx, filter).Decode(&result)
if err != nil {
return nil, err
}
return result, nil
}
func (o *MongoUtils) FindMore(col string, filter bson.M) ([]bson.M, error) {
if o.Db == nil || o.Con == nil {
return nil, fmt.Errorf("没有初始化连接和数据库信息!")
}
table := o.Db.Collection(col)
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
ctx, _ = context.WithTimeout(context.Background(), 5*time.Second)
cur, err2 := table.Find(ctx, filter)
if err2 != nil {
fmt.Print(err2)
return nil, err2
}
defer cur.Close(ctx)
var resultArr []bson.M
for cur.Next(ctx) {
var result bson.M
err3 := cur.Decode(&result)
if err3 != nil {
return nil, err3
}
resultArr = append(resultArr, result)
}
return resultArr, nil
}
func (o *MongoUtils) FindMoreByPipe(col string, pipe []bson.M) ([]bson.M, error) {
if o.Db == nil || o.Con == nil {
return nil, fmt.Errorf("没有初始化连接和数据库信息!")
}
table := o.Db.Collection(col)
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
ctx, _ = context.WithTimeout(context.Background(), 5*time.Second)
cur, err2 := table.Aggregate(ctx, pipe)
if err2 != nil {
fmt.Print(err2)
return nil, err2
}
defer cur.Close(ctx)
var resultArr []bson.M
for cur.Next(ctx) {
var result bson.M
err3 := cur.Decode(&result)
if err3 != nil {
return nil, err3
}
resultArr = append(resultArr, result)
}
return resultArr, nil
}
func createRow(insert interface{}, dbName string, tableName string) interface{} {
utils := MongoUtils{}
utils.OpenConn()
defer utils.Con.Disconnect(context.Background())
utils.SetDb(dbName)
col := utils.Db.Collection(tableName)
suc := true
_ = utils.Con.UseSession(context.Background(), func(ses mongo.SessionContext) error {
_ = ses.StartTransaction()
_, err := col.InsertOne(context.Background(), insert)
if err != nil {
suc = false
_ = ses.AbortTransaction(ses)
fmt.Println(err)
return err
}
_ = ses.CommitTransaction(ses)
return nil
})
if !suc {
return "err"
}
//b, _ := json.Marshal(insert)
//res := string(b)
return insert
}
func FindAllSelected(filter bson.M, dbName string, tableName string) []bson.M {
utils := MongoUtils{}
utils.OpenConn()
utils.SetDb(dbName)
result, err := utils.FindMore(tableName, bson.M(filter))
if err != nil {
logging.Info(err)
}
return result
}
func FindOneSelected(filter bson.M, dbName string, tableName string) bson.M {
utils := MongoUtils{}
utils.OpenConn()
utils.SetDb(dbName)
result, err := utils.FindOne(tableName, bson.M(filter))
if err != nil {
logging.Info(err)
}
return result
}
func UpdateAll(filter bson.M, update bson.M, dbName string, tableName string) int64 {
utils := MongoUtils{}
utils.OpenConn()
utils.SetDb(dbName)
result, err := utils.UpdateAll(tableName, filter, update)
if err != nil {
logging.Info(err)
}
return result
}
func FindByPipe(pipe []bson.M, dbName string, tableName string) []bson.M {
utils := MongoUtils{}
utils.OpenConn()
utils.SetDb(dbName)
result, err := utils.FindMoreByPipe(tableName, pipe)
if err != nil {
logging.Info(err)
}
return result
}
func (o *MongoUtils) createUniqueIndex(col string, keys ...string) {
table := o.Db.Collection(col)
opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
indexView := table.Indexes()
keysDoc := bsonx.Doc{}
// 复合索引
for _, key := range keys {
if strings.HasPrefix(key, "-") {
keysDoc = keysDoc.Append(strings.TrimLeft(key, "-"), bsonx.Int32(-1))
} else {
keysDoc = keysDoc.Append(key, bsonx.Int32(1))
}
}
// 创建索引
result, err := indexView.CreateOne(
context.Background(),
mongo.IndexModel{
Keys: keysDoc,
Options: options.Index().SetUnique(true),
},
opts,
)
if result == "" || err != nil {
log.Fatalf("EnsureIndex error:%v", err)
}
}