Example
package main
import (
"fmt"
"time"
log "github.com/Sirupsen/logrus"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var mongoConn *mgo.Session
type MongoDB_Conn struct {
Host string `json:"Host"`
Port string `json:"Port"`
User string `json:"User"`
Pass string `json:"Pass"`
DB string `json:"DB"`
}
func MongoConn(mdb MongoDB_Conn) (*mgo.Session, string, error) {
if mongoConn != nil {
if mongoConn.Ping() == nil {
return mongoConn, nil
}
}
user := mdb.User
pass := mdb.Pass
host := mdb.Host
port := mdb.Port
db := mdb.DB
if host == "" || port == "" || db == "" {
log.Fatal("Host or port or db is nil")
}
url := fmt.Sprintf("mongodb://%s:%s@%s:%s/%s", user, pass, host, port, db)
if user == "" {
url = fmt.Sprintf("mongodb://%s:%s/%s", host, port, db)
}
mongo, err := mgo.DialWithTimeout(url, 3*time.Second)
if err != nil {
log.Errorf("Mongo Conn Error: [%v], Mongo ConnUrl: [%v]",
err, url)
errTextReturn := fmt.Sprintf("Mongo Conn Error: [%v]", err)
return &mgo.Session{}, errors.New(errTextReturn)
}
mongoConn = mongo
return mongoConn, nil
}
func MongoInsert(dbName, C string, data interface{}) error {
mongo, err := MongoConn()
if err != nil {
log.Error(err)
return err
}
db := mongo.DB(dbName)
collection := db.C(C)
err = collection.Insert(data)
if err != nil {
return err
}
return nil
}
func MongoRemove(dbName, C string, selector bson.M) error {
mongo, err := MongoConn()
if err != nil {
log.Error(err)
return err
}
db := mongo.DB(dbName)
collection := db.C(C)
err = collection.Remove(selector)
if err != nil {
return err
}
return nil
}
func MongoFind(dbName, C string, query, selector bson.M) ([]interface{}, error) {
mongo, err := MongoConn()
if err != nil {
return nil, err
}
db := mongo.DB(dbName)
collection := db.C(C)
result := make([]interface{}, 0)
err = collection.Find(query).Select(selector).All(&result)
return result, err
}
func MongoUpdate(dbName, C string, selector bson.M, update interface{}) error {
mongo, err := MongoConn()
if err != nil {
log.Error(err)
return err
}
db := mongo.DB(dbName)
collection := db.C(C)
err = collection.Update(selector, update)
if err != nil {
return err
}
return nil
}