MongoDB
Less than 1 minutecomponentmongo
Overview
mgo
is a simple wrapper library based on the official MongoDB Go Driver.
MongoDB Configuration
Enable MongoDB support in the YAML configuration file under the configs
directory:
# database setting
database:
driver: "mongodb"
mongodb:
# dsn format, <username>:<password>@<hostname1>:<port1>[,<hostname2>:<port2>,......]/<db>?[k=v& ......]
dsn: "root:123456@192.168.3.37:27017/account?connectTimeoutMS=15000"
MongoDB Initialization
import "github.com/go-dev-frame/sponge/pkg/mgo"
// dsn document: https://www.mongodb.com/docs/manual/reference/connection-string/
// case 1: specify options in dsn
db, err := mgo.Init("mongodb://root:123456@192.168.3.37:27017/account?socketTimeoutMS=30000&maxPoolSize=100&minPoolSize=1&maxConnIdleTimeMS=300000")
// case 2: specify options in code
db, err := mgo.Init("mongodb://root:123456@192.168.3.37:27017/account",
mgo.WithOption().SetMaxPoolSize(100),
mgo.WithOption().SetMinPoolSize(1),
mgo.WithOption().SetMaxConnIdleTime(5*time.Minute),
mgo.WithOption().SetSocketTimeout(30*time.Second),
)
// close mongodb
defer mgo.Close(db)