| 12
 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
 
 | package models
 import (
 "log"
 
 "github.com/globalsign/mgo"
 "github.com/globalsign/mgo/bson"
 )
 
 const (
 host     = "localhost:27017"
 source   = ""
 username = ""
 password = ""
 )
 
 var session *mgo.Session
 
 func init() {
 dialInfo := &mgo.DialInfo{
 Addrs:    []string{host},
 Source:   source,
 Username: username,
 Password: password,
 }
 
 s, err := mgo.DialWithInfo(dialInfo)
 if err != nil {
 log.Fatalln("Error: ", err)
 }
 
 session = s
 }
 
 func connect(db string, collection string) (*mgo.Session, *mgo.Collection) {
 
 s := session.Copy()
 
 c := s.DB(db).C(collection)
 
 return s, c
 }
 
 
 func FindAll(db string, collection string, query interface{}, selector interface{}, result interface{}) error {
 s, c := connect(db, collection)
 
 defer s.Close()
 
 return c.Find(query).Select(selector).All(result)
 }
 
 
 func Find(db string, collection string, query interface{}, selector interface{}, result interface{}) error {
 s, c := connect(db, collection)
 
 defer s.Close()
 
 return c.Find(query).Select(selector).One(result)
 }
 
 
 func FindByID(db string, collection string, id string, result interface{}) error {
 s, c := connect(db, collection)
 
 defer s.Close()
 
 return c.FindId(bson.ObjectIdHex(id)).One(result)
 }
 
 
 func Insert(db string, collection string, docs ...interface{}) error {
 s, c := connect(db, collection)
 
 defer s.Close()
 
 return c.Insert(docs...)
 }
 
 
 func Update(db string, collection string, selector interface{}, update interface{}) error {
 s, c := connect(db, collection)
 
 defer s.Close()
 
 return c.Update(selector, update)
 }
 
 
 func UpdateByID(db string, collection string, id string, update interface{}) error {
 s, c := connect(db, collection)
 
 defer s.Close()
 
 return c.UpdateId(bson.ObjectIdHex(id), update)
 }
 
 
 func Remove(db string, collection string, selector interface{}) error {
 s, c := connect(db, collection)
 
 defer s.Close()
 
 return c.Remove(selector)
 }
 
 
 func RemoveByID(db string, collection string, id string) error {
 s, c := connect(db, collection)
 
 defer s.Close()
 
 return c.RemoveId(bson.ObjectIdHex(id))
 }
 
 |