- 方法1
//-----------------c1.go-------------------
package conf
import (
"gopkg.in/mgo.v2"
"fmt"
)
func Ccdb2() (*mgo.Database, *mgo.Session) {
mgo_url := "mongodb://t1:t1@localhost:27017/test?authMechanism=SCRAM-SHA-1"
session, err := mgo.Dial(mgo_url)
session.SetMode(mgo.Monotonic, true)
db := session.DB("test") //数据库名称
if err != nil {
fmt.Println("------连接数据库失败------------")
panic(err)
}
fmt.Println("------ConnectionDb-----2-------")
return db, session
}
func ColoseDb() {
_, session := ConnectionDb()
defer session.Close()
}
func PersonDocument() *mgo.Collection {
db, _ := Ccdb2()
conn := db.C("person")
return conn
}
//-----------------c2.go-------------------
//查询出一个集合
func GetPeople(w http.ResponseWriter, req *http.Request) {
fmt.Println("\n------------查询出一个集合--------------")
var person []documents.Person
//conf.PersonDocument().Find(bson.M{"name": "aa"}).All(&person)
conf.PersonDocument().Find(nil).All(&person)
for _, value := range person {
fmt.Println(value.Name)
}
conf.ColoseDb()//关闭数据库
json.NewEncoder(w).Encode(person)
}
- 方法2
//-----------------c1.go-------------------
package conf
import (
"gopkg.in/mgo.v2"
"fmt"
)
func Ccdb2() (*mgo.Database, *mgo.Session) {
mgo_url := "mongodb://t1:t1@localhost:27017/test" // 可以代替下面两步
session, err := mgo.Dial(mgo_url)
session.SetMode(mgo.Monotonic, true)
db := session.DB("test") //数据库名称
if err != nil {
fmt.Println("------连接数据库失败------------")
panic(err)
}
fmt.Println("------ConnectionDb-----2-------")
return db, session
}
func ColoseDb() {
_, session := ConnectionDb()
defer session.Close()
}
func PersonDocument() *mgo.Collection {
db, _ := Ccdb2()
conn := db.C("person")
return conn
}
//-----------------c2.go-------------------
//查询出一个集合
func GetPeople(w http.ResponseWriter, req *http.Request) {
fmt.Println("\n------------查询出一个集合--------------")
var person []documents.Person
//conf.PersonDocument().Find(bson.M{"name": "aa"}).All(&person)
conf.PersonDocument().Find(nil).All(&person)
for _, value := range person {
fmt.Println(value.Name)
}
conf.ColoseDb()//关闭数据库
json.NewEncoder(w).Encode(person)
}
- 方法3
//-----------------c1.go-------------------
package conf
import (
"gopkg.in/mgo.v2"
"fmt"
)
func Ccdb2() (*mgo.Database, *mgo.Session) {
mgo_url := "localhost:27017"//登录地址 第一步
session, err := mgo.Dial(mgo_url)
session.SetMode(mgo.Monotonic, true)
err = session.DB("test").Login("t1", "t1")//登录 认证 第二步
if err != nil {
panic(err)
}
db := session.DB("test") //数据库名称
if err != nil {
fmt.Println("------连接数据库失败------------")
panic(err)
}
fmt.Println("------ConnectionDb-----2-------")
return db, session
}
func ColoseDb() {
_, session := ConnectionDb()
defer session.Close()
}
func PersonDocument() *mgo.Collection {
db, _ := Ccdb2()
conn := db.C("person")
return conn
}
//-----------------c2.go-------------------
//查询出一个集合
func GetPeople(w http.ResponseWriter, req *http.Request) {
fmt.Println("\n------------查询出一个集合--------------")
var person []documents.Person
//conf.PersonDocument().Find(bson.M{"name": "aa"}).All(&person)
conf.PersonDocument().Find(nil).All(&person)
for _, value := range person {
fmt.Println(value.Name)
}
conf.ColoseDb()//关闭数据库
json.NewEncoder(w).Encode(person)
}
- 方法4
//-----------------c1.go-------------------
func PersonDocument() *mgo.Collection {
dail_info := &mgo.DialInfo{
Addrs: []string{"127.0.0.1"},
Direct: false,
Timeout: time.Second * 1,
Database: "test",
Source: "test",
Username: "t1",
Password: "t1",
PoolLimit: 1024,
}
session, err := mgo.DialWithInfo(dail_info)
if err != nil {
fmt.Printf("mgo dail error[%s]\n", err.Error())
}
defer session.Close()
// set mode
session.SetMode(mgo.Monotonic, true)
c := session.DB("test").C("person")
return c
}
//-----------------c2.go-------------------
//查询出一个集合
func GetPeople(w http.ResponseWriter, req *http.Request) {
fmt.Println("\n------------查询出一个集合--------------")
var person []documents.Person
//conf.PersonDocument().Find(bson.M{"name": "aa"}).All(&person)
conf.PersonDocument().Find(nil).All(&person)
for _, value := range person {
fmt.Println(value.Name)
}
conf.ColoseDb()//关闭数据库
json.NewEncoder(w).Encode(person)
}
网友评论