├── README.md └── mongo ├── dbop.go └── mongodb └── mongodb.go /README.md: -------------------------------------------------------------------------------- 1 | # goutil 2 | 对 [mgo](https://godoc.org/github.com/globalsign/mgo)关于MongoDB的一些基础操作做一下封装,便于直接调用 3 | 4 | * 插入一个或多个`document` 5 | 6 | ``` 7 | func Insert(db, collection string, docs ...interface{}) error 8 | ``` 9 | 10 | * 查询一个满足条件的`document` 11 | 12 | **query**查询条件 `bson.M{"_id":"id"}` 13 | **selector**相当于MongoDB中的projection,查询结果中是否显示某个字段 `bson.M{"_id":0}` 14 | 15 | ``` 16 | func FindOne(db, collection string, query, selector, result interface{}) error 17 | ``` 18 | 19 | * 查询满足条件的所有结果 20 | 21 | ``` 22 | func FindAll(db, collection string, query, selector, result interface{}) error 23 | ``` 24 | 25 | * 对查询结果分页处理 26 | 27 | ``` 28 | func FindPage(db, collection string, page, limit int, query, selector, result interface{}) error 29 | ``` 30 | 31 | * 查询满足条件的 `cursor` 32 | 33 | ``` 34 | func FindIter(db, collection string, query interface{}) *mgo.Iter 35 | ``` 36 | 37 | * 更新满足条件的一个`document` 38 | 39 | ``` 40 | func Update(db, collection string, selector, update interface{}) error 41 | ``` 42 | 43 | * 更新满足条件的所有的`docuement` 44 | 45 | ``` 46 | func UpdateAll(db, collection string, selector, update interface{}) error 47 | ``` 48 | 49 | * 更新,如果不存在就插入一个新的`document` 50 | 51 | ``` 52 | func Upsert(db, collection string, selector, update interface{}) error 53 | ``` 54 | 55 | * 删除满足条件的一个 `document` 56 | 57 | ``` 58 | func Remove(db, collection string, selector interface{}) error 59 | ``` 60 | 61 | * 删除满足条件的所有的`document` 62 | 63 | ``` 64 | func RemoveAll(db, collection string, selector interface{}) error 65 | ``` 66 | 67 | * 批量的插入 68 | 69 | ``` 70 | func BulkInsert(db, collection string, docs ...interface{}) (*mgo.BulkResult, error) 71 | ``` 72 | 73 | * 批量更新 74 | 75 | ``` 76 | func BulkUpdate(db, collection string, pairs ...interface{}) (*mgo.BulkResult, error) 77 | ``` 78 | 79 | * 批量更新所有 80 | 81 | ``` 82 | func BulkUpdateAll(db, collection string, pairs ...interface{}) (*mgo.BulkResult, error) 83 | ``` 84 | 85 | * 批量删除 86 | 87 | ``` 88 | func BulkRemove(db, collection string, selector ...interface{}) (*mgo.BulkResult, error) 89 | ``` 90 | 91 | * 批量删除所有 92 | 93 | ``` 94 | func BulkRemoveAll(db, collection string, selector ...interface{}) (*mgo.BulkResult, error) 95 | ``` 96 | 97 | * 聚合操作查找所有的 98 | 99 | ``` 100 | func PipeAll(db, collection string, pipeline, result interface{}, allowDiskUse bool) error 101 | ``` 102 | 103 | * 聚合操作查找一个 104 | 105 | ``` 106 | func PipeOne(db, collection string, pipeline, result interface{}, allowDiskUse bool) error 107 | ``` 108 | 109 | * 聚合操作查找`cursor` 110 | 111 | ``` 112 | func PipeIter(db, collection string, pipeline interface{}, allowDiskUse bool) *mgo.Iter 113 | ``` 114 | 115 | * Document是否为空 116 | 117 | ``` 118 | func IsEmpty(db, collection string) bool 119 | ``` 120 | 121 | * 查询满足条件的数量 122 | 123 | ``` 124 | func Count(db, collection string, query interface{}) (int, error) 125 | ``` 126 | -------------------------------------------------------------------------------- /mongo/dbop.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | db "mongo/mongodb" 6 | "time" 7 | 8 | "github.com/globalsign/mgo/bson" 9 | ) 10 | 11 | type Data struct { 12 | Id bson.ObjectId `bson:"_id"` 13 | Title string `bson:"title"` 14 | Des string `bson:"des"` 15 | Content string `bson:"content"` 16 | Date time.Time `bson:"date"` 17 | } 18 | 19 | const ( 20 | database = "Test" 21 | collection = "TestModel" 22 | ) 23 | 24 | // Examples : the operation of mgo to MongoDB 25 | func main() { 26 | //insert one document 27 | data := &Data{ 28 | Id: bson.NewObjectId(), 29 | Title: "博客的标题 1", 30 | Des: "博客描述信息 1", 31 | Content: "博客的具体内容 1", 32 | Date: time.Now(), 33 | } 34 | 35 | err := db.Insert(database, collection, data) 36 | if err != nil { 37 | fmt.Println("insert one doc", err) 38 | } 39 | 40 | // find one with all fields 41 | var result Data 42 | err = db.FindOne(database, collection, bson.M{"_id": bson.ObjectIdHex("5b3db2334d661ff46ee14b9c")}, nil, &result) 43 | fmt.Println("find one with all fields", result) 44 | 45 | // find one without id field 46 | var result1 Data 47 | err = db.FindOne(database, collection, bson.M{"_id": bson.ObjectIdHex("5b3db2334d661ff46ee14b9c")}, bson.M{"_id": 0}, &result1) 48 | fmt.Println("find one without id field", result1) 49 | 50 | //find all documents 51 | var allResult []Data 52 | err = db.FindAll(database, collection, nil, nil, &allResult) 53 | fmt.Println("find all docs", allResult) 54 | 55 | // find all documents with query and selector 56 | var allResult1 []Data 57 | err = db.FindAll(database, collection, bson.M{"title": "博客的标题 1"}, bson.M{"_id": 0}, &allResult1) 58 | fmt.Println("find all docs with query and selector", allResult1) 59 | 60 | //find documents with page and limit 61 | var resultWithPage []Data 62 | err = db.FindPage(database, collection, 0, 4, nil, bson.M{"_id": 0}, &resultWithPage) 63 | fmt.Println("find docs with page and limit", resultWithPage) 64 | 65 | //find the cursor 66 | var iterAll []Data 67 | iter := db.FindIter(database, collection, nil) 68 | err = iter.All(&iterAll) 69 | fmt.Println("find cursor ", iterAll) 70 | 71 | //update one document 72 | err = db.Update(database, collection, bson.M{"_id": bson.ObjectIdHex("5b3db2334d661ff46ee14b9c")}, bson.M{"$set": bson.M{ 73 | "title": "更新后的标题", 74 | "des": "更新后的描述信息", 75 | "date": time.Now(), 76 | }}) 77 | 78 | if err != nil { 79 | fmt.Println("upate one error", err) 80 | } 81 | 82 | //update all docments 83 | /*err = db.UpdateAll(database, collection, nil, bson.M{"$set": bson.M{ 84 | "title": "更新所有的标题", 85 | "date": time.Now(), 86 | }}) 87 | if err != nil { 88 | fmt.Println("update all docs error ", err) 89 | }*/ 90 | 91 | //delete one docment 92 | err = db.Remove(database, collection, bson.M{"_id": bson.ObjectIdHex("5b3db2334d661ff46ee14b99")}) 93 | if err != nil { 94 | fmt.Println("remove one doc error", err) 95 | } 96 | 97 | //upsert the docment 98 | err = db.Upsert(database, collection, bson.M{"title": "Title Upsert"}, bson.M{"$set": bson.M{ 99 | "des": "描述Upsert", 100 | "date": time.Now(), 101 | "Content": "内容Upsert", 102 | }}) 103 | if err != nil { 104 | fmt.Println("upsert docment error", err) 105 | } 106 | 107 | //bulk insert docments 108 | d1 := &Data{ 109 | Id: bson.NewObjectId(), 110 | Title: "bulk title", 111 | Des: "bulk Des", 112 | Content: "bulk content", 113 | Date: time.Now(), 114 | } 115 | d2 := &Data{ 116 | Id: bson.NewObjectId(), 117 | Title: "bulk title", 118 | Des: "bulk Des", 119 | Content: "bulk content", 120 | Date: time.Now(), 121 | } 122 | 123 | insertResult, _ := db.BulkInsert(database, collection, d1, d2) 124 | fmt.Println("bulk insert docs", insertResult) 125 | 126 | //bulk update 127 | up1 := bson.M{"title": "bulk update title"} 128 | up2 := bson.M{"$set": bson.M{"title": "bulk update title"}} 129 | 130 | up3 := bson.M{"_id": bson.ObjectIdHex("5b3dbd7a9d5e3e314c93d150")} 131 | up4 := bson.M{"$set": bson.M{"des": "bulk update des"}} 132 | 133 | updateResult, _ := db.BulkUpdate(database, collection, up1, up2, up3, up4) 134 | fmt.Println("bulk update result", updateResult) 135 | 136 | //bulk update all 137 | up5 := bson.M{"title": "bulk title"} 138 | up6 := bson.M{"$set": bson.M{"title": "bulk update title"}} 139 | 140 | updateAllResult, _ := db.BulkUpdateAll(database, collection, up5, up6) 141 | fmt.Println("bulk update result", updateAllResult) 142 | 143 | } 144 | -------------------------------------------------------------------------------- /mongo/mongodb/mongodb.go: -------------------------------------------------------------------------------- 1 | package db 2 | 3 | import ( 4 | "log" 5 | "time" 6 | 7 | "github.com/globalsign/mgo" 8 | ) 9 | 10 | const ( 11 | dbhost = "127.0.0.1:27017" 12 | authdb = "admin" 13 | authuser = "user" 14 | authpass = "123456" 15 | timeout = 60 * time.Second 16 | poollimit = 4096 17 | ) 18 | 19 | var globalS *mgo.Session 20 | 21 | func init() { 22 | dialInfo := &mgo.DialInfo{ 23 | Addrs: []string{dbhost}, 24 | Timeout: timeout, 25 | Source: authdb, 26 | Username: authuser, 27 | Password: authpass, 28 | PoolLimit: poollimit, 29 | } 30 | 31 | s, err := mgo.DialWithInfo(dialInfo) 32 | if err != nil { 33 | log.Fatalf("Create Session: %s\n", err) 34 | } 35 | globalS = s 36 | } 37 | 38 | func connect(db, collection string) (*mgo.Session, *mgo.Collection) { 39 | ms := globalS.Copy() 40 | c := ms.DB(db).C(collection) 41 | ms.SetMode(mgo.Monotonic, true) 42 | return ms, c 43 | } 44 | 45 | func getDb(db string) (*mgo.Session, *mgo.Database) { 46 | ms := globalS.Copy() 47 | return ms, ms.DB(db) 48 | } 49 | 50 | func IsEmpty(db, collection string) bool { 51 | ms, c := connect(db, collection) 52 | defer ms.Close() 53 | count, err := c.Count() 54 | if err != nil { 55 | log.Fatal(err) 56 | } 57 | return count == 0 58 | } 59 | 60 | func Count(db, collection string, query interface{}) (int, error) { 61 | ms, c := connect(db, collection) 62 | defer ms.Close() 63 | return c.Find(query).Count() 64 | } 65 | 66 | func Insert(db, collection string, docs ...interface{}) error { 67 | ms, c := connect(db, collection) 68 | defer ms.Close() 69 | 70 | return c.Insert(docs...) 71 | } 72 | 73 | func FindOne(db, collection string, query, selector, result interface{}) error { 74 | ms, c := connect(db, collection) 75 | defer ms.Close() 76 | 77 | return c.Find(query).Select(selector).One(result) 78 | } 79 | 80 | func FindAll(db, collection string, query, selector, result interface{}) error { 81 | ms, c := connect(db, collection) 82 | defer ms.Close() 83 | 84 | return c.Find(query).Select(selector).All(result) 85 | } 86 | 87 | func FindPage(db, collection string, page, limit int, query, selector, result interface{}) error { 88 | ms, c := connect(db, collection) 89 | defer ms.Close() 90 | 91 | return c.Find(query).Select(selector).Skip(page * limit).Limit(limit).All(result) 92 | } 93 | 94 | func FindIter(db, collection string, query interface{}) *mgo.Iter { 95 | ms, c := connect(db, collection) 96 | defer ms.Close() 97 | 98 | return c.Find(query).Iter() 99 | } 100 | 101 | func Update(db, collection string, selector, update interface{}) error { 102 | ms, c := connect(db, collection) 103 | defer ms.Close() 104 | 105 | return c.Update(selector, update) 106 | } 107 | 108 | func Upsert(db, collection string, selector, update interface{}) error { 109 | ms, c := connect(db, collection) 110 | defer ms.Close() 111 | 112 | _, err := c.Upsert(selector, update) 113 | return err 114 | } 115 | 116 | func UpdateAll(db, collection string, selector, update interface{}) error { 117 | ms, c := connect(db, collection) 118 | defer ms.Close() 119 | 120 | _, err := c.UpdateAll(selector, update) 121 | return err 122 | } 123 | 124 | func Remove(db, collection string, selector interface{}) error { 125 | ms, c := connect(db, collection) 126 | defer ms.Close() 127 | 128 | return c.Remove(selector) 129 | } 130 | 131 | func RemoveAll(db, collection string, selector interface{}) error { 132 | ms, c := connect(db, collection) 133 | defer ms.Close() 134 | 135 | _, err := c.RemoveAll(selector) 136 | return err 137 | } 138 | 139 | //insert one or multi documents 140 | func BulkInsert(db, collection string, docs ...interface{}) (*mgo.BulkResult, error) { 141 | ms, c := connect(db, collection) 142 | defer ms.Close() 143 | bulk := c.Bulk() 144 | bulk.Insert(docs...) 145 | return bulk.Run() 146 | } 147 | 148 | func BulkRemove(db, collection string, selector ...interface{}) (*mgo.BulkResult, error) { 149 | ms, c := connect(db, collection) 150 | defer ms.Close() 151 | 152 | bulk := c.Bulk() 153 | bulk.Remove(selector...) 154 | return bulk.Run() 155 | } 156 | 157 | func BulkRemoveAll(db, collection string, selector ...interface{}) (*mgo.BulkResult, error) { 158 | ms, c := connect(db, collection) 159 | defer ms.Close() 160 | bulk := c.Bulk() 161 | bulk.RemoveAll(selector...) 162 | return bulk.Run() 163 | } 164 | 165 | func BulkUpdate(db, collection string, pairs ...interface{}) (*mgo.BulkResult, error) { 166 | ms, c := connect(db, collection) 167 | defer ms.Close() 168 | bulk := c.Bulk() 169 | bulk.Update(pairs...) 170 | return bulk.Run() 171 | } 172 | 173 | func BulkUpdateAll(db, collection string, pairs ...interface{}) (*mgo.BulkResult, error) { 174 | ms, c := connect(db, collection) 175 | defer ms.Close() 176 | bulk := c.Bulk() 177 | bulk.UpdateAll(pairs...) 178 | return bulk.Run() 179 | } 180 | 181 | func BulkUpsert(db, collection string, pairs ...interface{}) (*mgo.BulkResult, error) { 182 | ms, c := connect(db, collection) 183 | defer ms.Close() 184 | bulk := c.Bulk() 185 | bulk.Upsert(pairs...) 186 | return bulk.Run() 187 | } 188 | 189 | func PipeAll(db, collection string, pipeline, result interface{}, allowDiskUse bool) error { 190 | ms, c := connect(db, collection) 191 | defer ms.Close() 192 | var pipe *mgo.Pipe 193 | if allowDiskUse { 194 | pipe = c.Pipe(pipeline).AllowDiskUse() 195 | } else { 196 | pipe = c.Pipe(pipeline) 197 | } 198 | return pipe.All(result) 199 | } 200 | 201 | func PipeOne(db, collection string, pipeline, result interface{}, allowDiskUse bool) error { 202 | ms, c := connect(db, collection) 203 | defer ms.Close() 204 | var pipe *mgo.Pipe 205 | if allowDiskUse { 206 | pipe = c.Pipe(pipeline).AllowDiskUse() 207 | } else { 208 | pipe = c.Pipe(pipeline) 209 | } 210 | return pipe.One(result) 211 | } 212 | 213 | func PipeIter(db, collection string, pipeline interface{}, allowDiskUse bool) *mgo.Iter { 214 | ms, c := connect(db, collection) 215 | defer ms.Close() 216 | var pipe *mgo.Pipe 217 | if allowDiskUse { 218 | pipe = c.Pipe(pipeline).AllowDiskUse() 219 | } else { 220 | pipe = c.Pipe(pipeline) 221 | } 222 | 223 | return pipe.Iter() 224 | 225 | } 226 | 227 | func Explain(db, collection string, pipeline, result interface{}) error { 228 | ms, c := connect(db, collection) 229 | defer ms.Close() 230 | pipe := c.Pipe(pipeline) 231 | return pipe.Explain(result) 232 | } 233 | func GridFSCreate(db, prefix, name string) (*mgo.GridFile, error) { 234 | ms, d := getDb(db) 235 | defer ms.Close() 236 | gridFs := d.GridFS(prefix) 237 | return gridFs.Create(name) 238 | } 239 | 240 | func GridFSFindOne(db, prefix string, query, result interface{}) error { 241 | ms, d := getDb(db) 242 | defer ms.Close() 243 | gridFs := d.GridFS(prefix) 244 | return gridFs.Find(query).One(result) 245 | } 246 | 247 | func GridFSFindAll(db, prefix string, query, result interface{}) error { 248 | ms, d := getDb(db) 249 | defer ms.Close() 250 | gridFs := d.GridFS(prefix) 251 | return gridFs.Find(query).All(result) 252 | } 253 | 254 | func GridFSOpen(db, prefix, name string) (*mgo.GridFile, error) { 255 | ms, d := getDb(db) 256 | defer ms.Close() 257 | gridFs := d.GridFS(prefix) 258 | return gridFs.Open(name) 259 | } 260 | 261 | func GridFSRemove(db, prefix, name string) error { 262 | ms, d := getDb(db) 263 | defer ms.Close() 264 | gridFs := d.GridFS(prefix) 265 | return gridFs.Remove(name) 266 | } 267 | --------------------------------------------------------------------------------