forked from GhMartingit/xk6-mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.go
256 lines (217 loc) · 7.2 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package xk6_mongo
import (
"context"
"log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
k6modules "go.k6.io/k6/js/modules"
)
// Register the extension on module initialization, available to
// import from JS as "k6/x/mongo".
func init() {
k6modules.Register("k6/x/mongo", new(Mongo))
}
// Mongo is the k6 extension for a Mongo client.
type Mongo struct{}
// Client is the Mongo client wrapper.
type Client struct {
client *mongo.Client
}
type UpsertOneModel struct {
Query interface{} `json:"query"`
Update interface{} `json:"update"`
}
// NewClient represents the Client constructor (i.e. `new mongo.Client()`) and
// returns a new Mongo client object.
// connURI -> mongodb://username:password@address:port/db?connect=direct
func (*Mongo) NewClient(connURI string) *Client {
log.Print("start creating new client")
clientOptions := options.Client().ApplyURI(connURI)
client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
log.Printf("Error while establishing a connection to MongoDB: %v", err)
return nil
}
log.Print("created new client")
return &Client{client: client}
}
func (c *Client) Insert(database string, collection string, doc interface{}) error {
db := c.client.Database(database)
col := db.Collection(collection)
_, err := col.InsertOne(context.Background(), doc)
if err != nil {
log.Printf("Error while inserting document: %v", err)
return err
}
log.Print("Document inserted successfully")
return nil
}
func (c *Client) InsertMany(database string, collection string, docs []interface{}) error {
db := c.client.Database(database)
col := db.Collection(collection)
_, err := col.InsertMany(context.Background(), docs)
if err != nil {
log.Printf("Error while inserting multiple documents: %v", err)
return err
}
return nil
}
func (c *Client) Upsert(database string, collection string, filter interface{}, upsert interface{}) error {
db := c.client.Database(database)
col := db.Collection(collection)
opts := options.Update().SetUpsert(true)
_, err := col.UpdateOne(context.Background(), filter, upsert, opts)
if err != nil {
log.Printf("Error while performing upsert: %v", err)
return err
}
return nil
}
func (c *Client) Find(database string, collection string, filter interface{}, sort interface{}, limit int64) ([]bson.M, error) {
db := c.client.Database(database)
col := db.Collection(collection)
opts := options.Find().SetSort(sort).SetLimit(limit)
cur, err := col.Find(context.Background(), filter, opts)
if err != nil {
log.Printf("Error while finding documents: %v", err)
return nil, err
}
var results []bson.M
if err = cur.All(context.Background(), &results); err != nil {
log.Printf("Error while decoding documents: %v", err)
return nil, err
}
return results, nil
}
func (c *Client) Aggregate(database string, collection string, pipeline interface{}) ([]bson.M, error) {
db := c.client.Database(database)
col := db.Collection(collection)
cur, err := col.Aggregate(context.Background(), pipeline)
if err != nil {
log.Printf("Error while aggregating: %v", err)
return nil, err
}
var results []bson.M
if err = cur.All(context.Background(), &results); err != nil {
log.Printf("Error while decoding documents: %v", err)
return nil, err
}
return results, nil
}
func (c *Client) FindOne(database string, collection string, filter map[string]string) (bson.M, error) {
db := c.client.Database(database)
col := db.Collection(collection)
var result bson.M
err := col.FindOne(context.Background(), filter).Decode(&result)
if err != nil {
log.Printf("Error while finding the document: %v", err)
return nil, err
}
return result, nil
}
func (c *Client) UpdateOne(database string, collection string, filter interface{}, data bson.D) error {
db := c.client.Database(database)
col := db.Collection(collection)
_, err := col.UpdateOne(context.Background(), filter, data)
if err != nil {
log.Printf("Error while updating the document: %v", err)
return err
}
return nil
}
func (c *Client) UpdateMany(database string, collection string, filter interface{}, data bson.D) error {
db := c.client.Database(database)
col := db.Collection(collection)
update := bson.D{{"$set", data}}
_, err := col.UpdateMany(context.Background(), filter, update)
if err != nil {
log.Printf("Error while updating the documents: %v", err)
return err
}
return nil
}
func (c *Client) FindAll(database string, collection string) ([]bson.M, error) {
db := c.client.Database(database)
col := db.Collection(collection)
cur, err := col.Find(context.Background(), bson.D{{}})
if err != nil {
log.Printf("Error while finding documents: %v", err)
return nil, err
}
var results []bson.M
if err = cur.All(context.Background(), &results); err != nil {
log.Printf("Error while decoding documents: %v", err)
return nil, err
}
return results, nil
}
func (c *Client) DeleteOne(database string, collection string, filter map[string]string) error {
db := c.client.Database(database)
col := db.Collection(collection)
_, err := col.DeleteOne(context.Background(), filter)
if err != nil {
log.Printf("Error while deleting the document: %v", err)
return err
}
return nil
}
func (c *Client) DeleteMany(database string, collection string, filter map[string]string) error {
db := c.client.Database(database)
col := db.Collection(collection)
_, err := col.DeleteMany(context.Background(), filter)
if err != nil {
log.Printf("Error while deleting the documents: %v", err)
return err
}
return nil
}
func (c *Client) Distinct(database string, collection string, field string, filter interface{}) ([]interface{}, error) {
db := c.client.Database(database)
col := db.Collection(collection)
result, err := col.Distinct(context.Background(), field, filter)
if err != nil {
log.Printf("Error while getting distinct values: %v", err)
return nil, err
}
return result, nil
}
func (c *Client) DropCollection(database string, collection string) error {
db := c.client.Database(database)
col := db.Collection(collection)
err := col.Drop(context.Background())
if err != nil {
log.Printf("Error while dropping the collection: %v", err)
return err
}
return nil
}
func (c *Client) CountDocuments(database string, collection string, filter interface{}) (int64, error) {
db := c.client.Database(database)
col := db.Collection(collection)
count, err := col.CountDocuments(context.Background(), filter)
if err != nil {
log.Printf("Error while counting documents: %v", err)
return 0, err
}
return count, nil
}
func (c *Client) FindOneAndUpdate(database string, collection string, filter interface{}, update interface{}) (*mongo.SingleResult, error) {
db := c.client.Database(database)
col := db.Collection(collection)
opts := options.FindOneAndUpdate().SetReturnDocument(options.After)
result := col.FindOneAndUpdate(context.Background(), filter, update, opts)
if result.Err() != nil {
log.Printf("Error while finding and updating document: %v", result.Err())
return nil, result.Err()
}
return result, nil
}
func (c *Client) Disconnect() error {
err := c.client.Disconnect(context.Background())
if err != nil {
log.Printf("Error while disconnecting from the database: %v", err)
return err
}
return nil
}