-
Notifications
You must be signed in to change notification settings - Fork 1
/
mgo.go
38 lines (29 loc) · 850 Bytes
/
mgo.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
package scouter
import (
"log"
"github.com/globalsign/mgo"
)
const (
MongoUrl = "mongodb://127.0.0.1:27017/scouter"
UserCollection = "users"
)
var mongoSession *mgo.Session
func init() {
session, err := mgo.Dial(MongoUrl)
if err != nil {
log.Fatal(err)
}
mongoSession = session
}
func CountCollectionRecords(collection string) (int, error) {
return mongoSession.DB("").C(collection).Count()
}
func InsertRecord(collection string, record interface{}) error {
return mongoSession.DB("").C(collection).Insert(record)
}
func UpsertRecord(collection string, id interface{}, record interface{}) (*mgo.ChangeInfo, error) {
return mongoSession.DB("").C(collection).Upsert(id, record)
}
func UpdateById(collection string, id interface{}, update interface{}) error {
return mongoSession.DB("").C(collection).UpdateId(id, update)
}