@@ -26,7 +26,6 @@ type MongoDatabase struct {
26
26
func InitMongoDatabase (host string , db_name string ) (* MongoDatabase , error ) {
27
27
db := MongoDatabase {}
28
28
err := db .Connect (host )
29
-
30
29
if err != nil {
31
30
return & db , err
32
31
}
@@ -40,11 +39,10 @@ func InitMongoDatabase(host string, db_name string) (*MongoDatabase, error) {
40
39
Open a session to the given mongo database
41
40
*/
42
41
func (db * MongoDatabase ) Connect (host string ) error {
43
- client_options := options .Client ().ApplyURI ("mongodb://" + host + ":27017" )
42
+ client_options := options .Client ().ApplyURI (host )
44
43
45
44
{
46
45
err := client_options .Validate ()
47
-
48
46
if err != nil {
49
47
// problems parsing host
50
48
return ErrConnection
@@ -62,7 +60,6 @@ func (db *MongoDatabase) Connect(host string) error {
62
60
ctx , cancel := context .WithTimeout (context .Background (), 60 * time .Second )
63
61
defer cancel ()
64
62
client , err := mongo .Connect (ctx , client_options )
65
-
66
63
if err != nil {
67
64
// failed to connect to database
68
65
return ErrConnection
@@ -89,7 +86,6 @@ func (db *MongoDatabase) GetRaw() *mongo.Client {
89
86
*/
90
87
func (db * MongoDatabase ) GetSession () (* mongo.Session , error ) {
91
88
session , err := db .client .StartSession (nil )
92
-
93
89
if err != nil {
94
90
return nil , err
95
91
}
@@ -107,10 +103,14 @@ func (db *MongoDatabase) GetNewContext() (context.Context, context.CancelFunc) {
107
103
/*
108
104
Find one element matching the given query parameters
109
105
*/
110
- func (db * MongoDatabase ) FindOne (collection_name string , query interface {}, result interface {}, session * mongo.SessionContext ) error {
106
+ func (db * MongoDatabase ) FindOne (
107
+ collection_name string ,
108
+ query interface {},
109
+ result interface {},
110
+ session * mongo.SessionContext ,
111
+ ) error {
111
112
if session == nil {
112
113
s , err := db .GetSession ()
113
-
114
114
if err != nil {
115
115
return convertMgoError (err )
116
116
}
@@ -134,10 +134,14 @@ func (db *MongoDatabase) FindOne(collection_name string, query interface{}, resu
134
134
/*
135
135
Finds and deletes one element matching the given query parameters atomically
136
136
*/
137
- func (db * MongoDatabase ) FindOneAndDelete (collection_name string , query interface {}, result interface {}, session * mongo.SessionContext ) error {
137
+ func (db * MongoDatabase ) FindOneAndDelete (
138
+ collection_name string ,
139
+ query interface {},
140
+ result interface {},
141
+ session * mongo.SessionContext ,
142
+ ) error {
138
143
if session == nil {
139
144
s , err := db .GetSession ()
140
-
141
145
if err != nil {
142
146
return convertMgoError (err )
143
147
}
@@ -158,10 +162,17 @@ func (db *MongoDatabase) FindOneAndDelete(collection_name string, query interfac
158
162
return convertMgoError (err )
159
163
}
160
164
161
- func (db * MongoDatabase ) FindOneAndUpdate (collection_name string , query interface {}, update interface {}, result interface {}, return_new_doc bool , upsert bool , session * mongo.SessionContext ) error {
165
+ func (db * MongoDatabase ) FindOneAndUpdate (
166
+ collection_name string ,
167
+ query interface {},
168
+ update interface {},
169
+ result interface {},
170
+ return_new_doc bool ,
171
+ upsert bool ,
172
+ session * mongo.SessionContext ,
173
+ ) error {
162
174
if session == nil {
163
175
s , err := db .GetSession ()
164
-
165
176
if err != nil {
166
177
return convertMgoError (err )
167
178
}
@@ -189,10 +200,17 @@ func (db *MongoDatabase) FindOneAndUpdate(collection_name string, query interfac
189
200
return convertMgoError (err )
190
201
}
191
202
192
- func (db * MongoDatabase ) FindOneAndReplace (collection_name string , query interface {}, update interface {}, result interface {}, return_new_doc bool , upsert bool , session * mongo.SessionContext ) error {
203
+ func (db * MongoDatabase ) FindOneAndReplace (
204
+ collection_name string ,
205
+ query interface {},
206
+ update interface {},
207
+ result interface {},
208
+ return_new_doc bool ,
209
+ upsert bool ,
210
+ session * mongo.SessionContext ,
211
+ ) error {
193
212
if session == nil {
194
213
s , err := db .GetSession ()
195
-
196
214
if err != nil {
197
215
return convertMgoError (err )
198
216
}
@@ -223,10 +241,14 @@ func (db *MongoDatabase) FindOneAndReplace(collection_name string, query interfa
223
241
/*
224
242
Find all elements matching the given query parameters
225
243
*/
226
- func (db * MongoDatabase ) FindAll (collection_name string , query interface {}, result interface {}, session * mongo.SessionContext ) error {
244
+ func (db * MongoDatabase ) FindAll (
245
+ collection_name string ,
246
+ query interface {},
247
+ result interface {},
248
+ session * mongo.SessionContext ,
249
+ ) error {
227
250
if session == nil {
228
251
s , err := db .GetSession ()
229
-
230
252
if err != nil {
231
253
return convertMgoError (err )
232
254
}
@@ -241,7 +263,6 @@ func (db *MongoDatabase) FindAll(collection_name string, query interface{}, resu
241
263
query = nilToEmptyBson (query )
242
264
243
265
cursor , err := db .client .Database (db .name ).Collection (collection_name ).Find (* session , query )
244
-
245
266
if err != nil {
246
267
return convertMgoError (err )
247
268
}
@@ -257,10 +278,15 @@ func (db *MongoDatabase) FindAll(collection_name string, query interface{}, resu
257
278
Find all elements matching the given query parameters, and sorts them based on given sort fields
258
279
The first sort field is highest priority, each subsequent field breaks ties
259
280
*/
260
- func (db * MongoDatabase ) FindAllSorted (collection_name string , query interface {}, sort_fields bson.D , result interface {}, session * mongo.SessionContext ) error {
281
+ func (db * MongoDatabase ) FindAllSorted (
282
+ collection_name string ,
283
+ query interface {},
284
+ sort_fields bson.D ,
285
+ result interface {},
286
+ session * mongo.SessionContext ,
287
+ ) error {
261
288
if session == nil {
262
289
s , err := db .GetSession ()
263
-
264
290
if err != nil {
265
291
return convertMgoError (err )
266
292
}
@@ -277,7 +303,6 @@ func (db *MongoDatabase) FindAllSorted(collection_name string, query interface{}
277
303
options := options .Find ().SetSort (sort_fields )
278
304
279
305
cursor , err := db .client .Database (db .name ).Collection (collection_name ).Find (* session , query , options )
280
-
281
306
if err != nil {
282
307
return convertMgoError (err )
283
308
}
@@ -295,7 +320,6 @@ func (db *MongoDatabase) FindAllSorted(collection_name string, query interface{}
295
320
func (db * MongoDatabase ) RemoveOne (collection_name string , query interface {}, session * mongo.SessionContext ) error {
296
321
if session == nil {
297
322
s , err := db .GetSession ()
298
-
299
323
if err != nil {
300
324
return convertMgoError (err )
301
325
}
@@ -317,10 +341,13 @@ func (db *MongoDatabase) RemoveOne(collection_name string, query interface{}, se
317
341
/*
318
342
Remove all elements matching the given query parameters
319
343
*/
320
- func (db * MongoDatabase ) RemoveAll (collection_name string , query interface {}, session * mongo.SessionContext ) (* ChangeResults , error ) {
344
+ func (db * MongoDatabase ) RemoveAll (
345
+ collection_name string ,
346
+ query interface {},
347
+ session * mongo.SessionContext ,
348
+ ) (* ChangeResults , error ) {
321
349
if session == nil {
322
350
s , err := db .GetSession ()
323
-
324
351
if err != nil {
325
352
return nil , convertMgoError (err )
326
353
}
@@ -335,7 +362,6 @@ func (db *MongoDatabase) RemoveAll(collection_name string, query interface{}, se
335
362
query = nilToEmptyBson (query )
336
363
337
364
res , err := db .client .Database (db .name ).Collection (collection_name ).DeleteMany (* session , query )
338
-
339
365
if err != nil {
340
366
return nil , convertMgoError (err )
341
367
}
@@ -354,7 +380,6 @@ func (db *MongoDatabase) RemoveAll(collection_name string, query interface{}, se
354
380
func (db * MongoDatabase ) Insert (collection_name string , item interface {}, session * mongo.SessionContext ) error {
355
381
if session == nil {
356
382
s , err := db .GetSession ()
357
-
358
383
if err != nil {
359
384
return convertMgoError (err )
360
385
}
@@ -377,10 +402,14 @@ func (db *MongoDatabase) Insert(collection_name string, item interface{}, sessio
377
402
Upsert the given item into the collection i.e.,
378
403
if the item exists, it is updated with the given values, else a new item with those values is created.
379
404
*/
380
- func (db * MongoDatabase ) Upsert (collection_name string , selector interface {}, update interface {}, session * mongo.SessionContext ) (* ChangeResults , error ) {
405
+ func (db * MongoDatabase ) Upsert (
406
+ collection_name string ,
407
+ selector interface {},
408
+ update interface {},
409
+ session * mongo.SessionContext ,
410
+ ) (* ChangeResults , error ) {
381
411
if session == nil {
382
412
s , err := db .GetSession ()
383
-
384
413
if err != nil {
385
414
return nil , convertMgoError (err )
386
415
}
@@ -397,7 +426,6 @@ func (db *MongoDatabase) Upsert(collection_name string, selector interface{}, up
397
426
options := options .Update ().SetUpsert (true )
398
427
399
428
res , err := db .client .Database (db .name ).Collection (collection_name ).UpdateOne (* session , selector , update , options )
400
-
401
429
if err != nil {
402
430
return nil , convertMgoError (err )
403
431
}
@@ -413,10 +441,14 @@ func (db *MongoDatabase) Upsert(collection_name string, selector interface{}, up
413
441
/*
414
442
Finds an item based on the given selector and updates it with the data in update
415
443
*/
416
- func (db * MongoDatabase ) Update (collection_name string , selector interface {}, update interface {}, session * mongo.SessionContext ) error {
444
+ func (db * MongoDatabase ) Update (
445
+ collection_name string ,
446
+ selector interface {},
447
+ update interface {},
448
+ session * mongo.SessionContext ,
449
+ ) error {
417
450
if session == nil {
418
451
s , err := db .GetSession ()
419
-
420
452
if err != nil {
421
453
return convertMgoError (err )
422
454
}
@@ -431,7 +463,6 @@ func (db *MongoDatabase) Update(collection_name string, selector interface{}, up
431
463
selector = nilToEmptyBson (selector )
432
464
433
465
res , err := db .client .Database (db .name ).Collection (collection_name ).UpdateOne (* session , selector , update )
434
-
435
466
if err != nil {
436
467
return convertMgoError (err )
437
468
}
@@ -446,10 +477,14 @@ func (db *MongoDatabase) Update(collection_name string, selector interface{}, up
446
477
/*
447
478
Finds all items based on the given selector and updates them with the data in update
448
479
*/
449
- func (db * MongoDatabase ) UpdateAll (collection_name string , selector interface {}, update interface {}, session * mongo.SessionContext ) (* ChangeResults , error ) {
480
+ func (db * MongoDatabase ) UpdateAll (
481
+ collection_name string ,
482
+ selector interface {},
483
+ update interface {},
484
+ session * mongo.SessionContext ,
485
+ ) (* ChangeResults , error ) {
450
486
if session == nil {
451
487
s , err := db .GetSession ()
452
-
453
488
if err != nil {
454
489
return nil , convertMgoError (err )
455
490
}
@@ -464,7 +499,6 @@ func (db *MongoDatabase) UpdateAll(collection_name string, selector interface{},
464
499
selector = nilToEmptyBson (selector )
465
500
466
501
res , err := db .client .Database (db .name ).Collection (collection_name ).UpdateMany (* session , selector , update )
467
-
468
502
if err != nil {
469
503
return nil , convertMgoError (err )
470
504
}
@@ -480,10 +514,15 @@ func (db *MongoDatabase) UpdateAll(collection_name string, selector interface{},
480
514
/*
481
515
Finds an item based on the given selector and replaces it with the data in update
482
516
*/
483
- func (db * MongoDatabase ) Replace (collection_name string , selector interface {}, update interface {}, upsert bool , session * mongo.SessionContext ) error {
517
+ func (db * MongoDatabase ) Replace (
518
+ collection_name string ,
519
+ selector interface {},
520
+ update interface {},
521
+ upsert bool ,
522
+ session * mongo.SessionContext ,
523
+ ) error {
484
524
if session == nil {
485
525
s , err := db .GetSession ()
486
-
487
526
if err != nil {
488
527
return convertMgoError (err )
489
528
}
@@ -500,7 +539,6 @@ func (db *MongoDatabase) Replace(collection_name string, selector interface{}, u
500
539
options := options .Replace ().SetUpsert (upsert )
501
540
502
541
res , err := db .client .Database (db .name ).Collection (collection_name ).ReplaceOne (* session , selector , update , options )
503
-
504
542
if err != nil {
505
543
return convertMgoError (err )
506
544
}
@@ -518,7 +556,6 @@ func (db *MongoDatabase) Replace(collection_name string, selector interface{}, u
518
556
func (db * MongoDatabase ) DropDatabase (session * mongo.SessionContext ) error {
519
557
if session == nil {
520
558
s , err := db .GetSession ()
521
-
522
559
if err != nil {
523
560
return convertMgoError (err )
524
561
}
@@ -538,10 +575,13 @@ func (db *MongoDatabase) DropDatabase(session *mongo.SessionContext) error {
538
575
/*
539
576
Returns a map of statistics for a given collection
540
577
*/
541
- func (db * MongoDatabase ) GetStats (collection_name string , fields []string , session * mongo.SessionContext ) (map [string ]interface {}, error ) {
578
+ func (db * MongoDatabase ) GetStats (
579
+ collection_name string ,
580
+ fields []string ,
581
+ session * mongo.SessionContext ,
582
+ ) (map [string ]interface {}, error ) {
542
583
if session == nil {
543
584
s , err := db .GetSession ()
544
-
545
585
if err != nil {
546
586
return nil , convertMgoError (err )
547
587
}
@@ -554,7 +594,6 @@ func (db *MongoDatabase) GetStats(collection_name string, fields []string, sessi
554
594
}
555
595
556
596
cursor , err := db .client .Database (db .name ).Collection (collection_name ).Find (* session , bson.D {})
557
-
558
597
if err != nil {
559
598
return nil , convertMgoError (err )
560
599
}
@@ -568,7 +607,6 @@ func (db *MongoDatabase) GetStats(collection_name string, fields []string, sessi
568
607
if err := cursor .Decode (& result ); err != nil {
569
608
count += 1
570
609
err := AddEntryToStats (stats , result , fields )
571
-
572
610
if err != nil {
573
611
return nil , convertMgoError (err )
574
612
}
0 commit comments