@@ -26,7 +26,6 @@ type MongoDatabase struct {
2626func InitMongoDatabase (host string , db_name string ) (* MongoDatabase , error ) {
2727 db := MongoDatabase {}
2828 err := db .Connect (host )
29-
3029 if err != nil {
3130 return & db , err
3231 }
@@ -40,11 +39,10 @@ func InitMongoDatabase(host string, db_name string) (*MongoDatabase, error) {
4039 Open a session to the given mongo database
4140*/
4241func (db * MongoDatabase ) Connect (host string ) error {
43- client_options := options .Client ().ApplyURI ("mongodb://" + host + ":27017" )
42+ client_options := options .Client ().ApplyURI (host )
4443
4544 {
4645 err := client_options .Validate ()
47-
4846 if err != nil {
4947 // problems parsing host
5048 return ErrConnection
@@ -62,7 +60,6 @@ func (db *MongoDatabase) Connect(host string) error {
6260 ctx , cancel := context .WithTimeout (context .Background (), 60 * time .Second )
6361 defer cancel ()
6462 client , err := mongo .Connect (ctx , client_options )
65-
6663 if err != nil {
6764 // failed to connect to database
6865 return ErrConnection
@@ -89,7 +86,6 @@ func (db *MongoDatabase) GetRaw() *mongo.Client {
8986*/
9087func (db * MongoDatabase ) GetSession () (* mongo.Session , error ) {
9188 session , err := db .client .StartSession (nil )
92-
9389 if err != nil {
9490 return nil , err
9591 }
@@ -107,10 +103,14 @@ func (db *MongoDatabase) GetNewContext() (context.Context, context.CancelFunc) {
107103/*
108104 Find one element matching the given query parameters
109105*/
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 {
111112 if session == nil {
112113 s , err := db .GetSession ()
113-
114114 if err != nil {
115115 return convertMgoError (err )
116116 }
@@ -134,10 +134,14 @@ func (db *MongoDatabase) FindOne(collection_name string, query interface{}, resu
134134/*
135135 Finds and deletes one element matching the given query parameters atomically
136136*/
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 {
138143 if session == nil {
139144 s , err := db .GetSession ()
140-
141145 if err != nil {
142146 return convertMgoError (err )
143147 }
@@ -158,10 +162,17 @@ func (db *MongoDatabase) FindOneAndDelete(collection_name string, query interfac
158162 return convertMgoError (err )
159163}
160164
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 {
162174 if session == nil {
163175 s , err := db .GetSession ()
164-
165176 if err != nil {
166177 return convertMgoError (err )
167178 }
@@ -189,10 +200,17 @@ func (db *MongoDatabase) FindOneAndUpdate(collection_name string, query interfac
189200 return convertMgoError (err )
190201}
191202
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 {
193212 if session == nil {
194213 s , err := db .GetSession ()
195-
196214 if err != nil {
197215 return convertMgoError (err )
198216 }
@@ -223,10 +241,14 @@ func (db *MongoDatabase) FindOneAndReplace(collection_name string, query interfa
223241/*
224242 Find all elements matching the given query parameters
225243*/
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 {
227250 if session == nil {
228251 s , err := db .GetSession ()
229-
230252 if err != nil {
231253 return convertMgoError (err )
232254 }
@@ -241,7 +263,6 @@ func (db *MongoDatabase) FindAll(collection_name string, query interface{}, resu
241263 query = nilToEmptyBson (query )
242264
243265 cursor , err := db .client .Database (db .name ).Collection (collection_name ).Find (* session , query )
244-
245266 if err != nil {
246267 return convertMgoError (err )
247268 }
@@ -257,10 +278,15 @@ func (db *MongoDatabase) FindAll(collection_name string, query interface{}, resu
257278 Find all elements matching the given query parameters, and sorts them based on given sort fields
258279 The first sort field is highest priority, each subsequent field breaks ties
259280*/
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 {
261288 if session == nil {
262289 s , err := db .GetSession ()
263-
264290 if err != nil {
265291 return convertMgoError (err )
266292 }
@@ -277,7 +303,6 @@ func (db *MongoDatabase) FindAllSorted(collection_name string, query interface{}
277303 options := options .Find ().SetSort (sort_fields )
278304
279305 cursor , err := db .client .Database (db .name ).Collection (collection_name ).Find (* session , query , options )
280-
281306 if err != nil {
282307 return convertMgoError (err )
283308 }
@@ -295,7 +320,6 @@ func (db *MongoDatabase) FindAllSorted(collection_name string, query interface{}
295320func (db * MongoDatabase ) RemoveOne (collection_name string , query interface {}, session * mongo.SessionContext ) error {
296321 if session == nil {
297322 s , err := db .GetSession ()
298-
299323 if err != nil {
300324 return convertMgoError (err )
301325 }
@@ -317,10 +341,13 @@ func (db *MongoDatabase) RemoveOne(collection_name string, query interface{}, se
317341/*
318342 Remove all elements matching the given query parameters
319343*/
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 ) {
321349 if session == nil {
322350 s , err := db .GetSession ()
323-
324351 if err != nil {
325352 return nil , convertMgoError (err )
326353 }
@@ -335,7 +362,6 @@ func (db *MongoDatabase) RemoveAll(collection_name string, query interface{}, se
335362 query = nilToEmptyBson (query )
336363
337364 res , err := db .client .Database (db .name ).Collection (collection_name ).DeleteMany (* session , query )
338-
339365 if err != nil {
340366 return nil , convertMgoError (err )
341367 }
@@ -354,7 +380,6 @@ func (db *MongoDatabase) RemoveAll(collection_name string, query interface{}, se
354380func (db * MongoDatabase ) Insert (collection_name string , item interface {}, session * mongo.SessionContext ) error {
355381 if session == nil {
356382 s , err := db .GetSession ()
357-
358383 if err != nil {
359384 return convertMgoError (err )
360385 }
@@ -377,10 +402,14 @@ func (db *MongoDatabase) Insert(collection_name string, item interface{}, sessio
377402 Upsert the given item into the collection i.e.,
378403 if the item exists, it is updated with the given values, else a new item with those values is created.
379404*/
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 ) {
381411 if session == nil {
382412 s , err := db .GetSession ()
383-
384413 if err != nil {
385414 return nil , convertMgoError (err )
386415 }
@@ -397,7 +426,6 @@ func (db *MongoDatabase) Upsert(collection_name string, selector interface{}, up
397426 options := options .Update ().SetUpsert (true )
398427
399428 res , err := db .client .Database (db .name ).Collection (collection_name ).UpdateOne (* session , selector , update , options )
400-
401429 if err != nil {
402430 return nil , convertMgoError (err )
403431 }
@@ -413,10 +441,14 @@ func (db *MongoDatabase) Upsert(collection_name string, selector interface{}, up
413441/*
414442 Finds an item based on the given selector and updates it with the data in update
415443*/
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 {
417450 if session == nil {
418451 s , err := db .GetSession ()
419-
420452 if err != nil {
421453 return convertMgoError (err )
422454 }
@@ -431,7 +463,6 @@ func (db *MongoDatabase) Update(collection_name string, selector interface{}, up
431463 selector = nilToEmptyBson (selector )
432464
433465 res , err := db .client .Database (db .name ).Collection (collection_name ).UpdateOne (* session , selector , update )
434-
435466 if err != nil {
436467 return convertMgoError (err )
437468 }
@@ -446,10 +477,14 @@ func (db *MongoDatabase) Update(collection_name string, selector interface{}, up
446477/*
447478 Finds all items based on the given selector and updates them with the data in update
448479*/
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 ) {
450486 if session == nil {
451487 s , err := db .GetSession ()
452-
453488 if err != nil {
454489 return nil , convertMgoError (err )
455490 }
@@ -464,7 +499,6 @@ func (db *MongoDatabase) UpdateAll(collection_name string, selector interface{},
464499 selector = nilToEmptyBson (selector )
465500
466501 res , err := db .client .Database (db .name ).Collection (collection_name ).UpdateMany (* session , selector , update )
467-
468502 if err != nil {
469503 return nil , convertMgoError (err )
470504 }
@@ -480,10 +514,15 @@ func (db *MongoDatabase) UpdateAll(collection_name string, selector interface{},
480514/*
481515 Finds an item based on the given selector and replaces it with the data in update
482516*/
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 {
484524 if session == nil {
485525 s , err := db .GetSession ()
486-
487526 if err != nil {
488527 return convertMgoError (err )
489528 }
@@ -500,7 +539,6 @@ func (db *MongoDatabase) Replace(collection_name string, selector interface{}, u
500539 options := options .Replace ().SetUpsert (upsert )
501540
502541 res , err := db .client .Database (db .name ).Collection (collection_name ).ReplaceOne (* session , selector , update , options )
503-
504542 if err != nil {
505543 return convertMgoError (err )
506544 }
@@ -518,7 +556,6 @@ func (db *MongoDatabase) Replace(collection_name string, selector interface{}, u
518556func (db * MongoDatabase ) DropDatabase (session * mongo.SessionContext ) error {
519557 if session == nil {
520558 s , err := db .GetSession ()
521-
522559 if err != nil {
523560 return convertMgoError (err )
524561 }
@@ -538,10 +575,13 @@ func (db *MongoDatabase) DropDatabase(session *mongo.SessionContext) error {
538575/*
539576 Returns a map of statistics for a given collection
540577*/
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 ) {
542583 if session == nil {
543584 s , err := db .GetSession ()
544-
545585 if err != nil {
546586 return nil , convertMgoError (err )
547587 }
@@ -554,7 +594,6 @@ func (db *MongoDatabase) GetStats(collection_name string, fields []string, sessi
554594 }
555595
556596 cursor , err := db .client .Database (db .name ).Collection (collection_name ).Find (* session , bson.D {})
557-
558597 if err != nil {
559598 return nil , convertMgoError (err )
560599 }
@@ -568,7 +607,6 @@ func (db *MongoDatabase) GetStats(collection_name string, fields []string, sessi
568607 if err := cursor .Decode (& result ); err != nil {
569608 count += 1
570609 err := AddEntryToStats (stats , result , fields )
571-
572610 if err != nil {
573611 return nil , convertMgoError (err )
574612 }
0 commit comments