1.系統(tǒng)環(huán)境
- Golang:go version go1.10.3 darwin/amd64
- OS:MacOS
- MongoDB: version: 3.4.4
2.Golang使用MongoDB
使用:gopkg.in/mgo.v2
獲取包:go get gopkg.in/mgo.v2
引入:import "gopkg.in/mgo.v2"
3.簡(jiǎn)單使用MongoDB
3.1 數(shù)據(jù)設(shè)計(jì)
3.1.1 數(shù)據(jù)庫設(shè)計(jì):
數(shù)據(jù)庫名:mydb_tutorial
集合名: t_student
數(shù)據(jù)集合:t_student字段說明
字段 | 類型 | 說明 |
---|---|---|
name | string | 姓名 |
age | int | 年齡 |
sid | string | 學(xué)號(hào) |
status | int | 狀態(tài):1正常,9臀脏,刪除 |
3.1.2結(jié)構(gòu)體設(shè)計(jì):
type Student struct {
Name string `bson: "name"`
Age int `bson: "age"`
Sid string `bson: "sid"`
Status int `bson: "status"`
}
type Per struct {
Per []Student
}
mgo簡(jiǎn)介
func Dial(url string) (*Session, error)
官方簡(jiǎn)介:Dial establishes a new session to the cluster identified by the given seed server(s).
3.2 插入數(shù)據(jù)
mongo, err := mgo.Dial("127.0.0.1") // 建立連接
defer mongo.Close()
if err != nil {
return false
}
client := mongo.DB("mydb_tutorial").C("t_student") //選擇數(shù)據(jù)庫和集合
//創(chuàng)建數(shù)據(jù)
data := Student{
Name: "seeta",
Age: 18,
Sid: "s20180907",
Status: 1,
}
//插入數(shù)據(jù)
cErr := client.Insert(&data)
if cErr != nil {
return false
}
return true
執(zhí)行該段程序揉稚,MongoDB會(huì)出現(xiàn)一條記錄:
3.3 查找數(shù)據(jù)
在3.2插入數(shù)據(jù)的基礎(chǔ)上搀玖,我們?cè)俨迦胍粭l數(shù)據(jù):
data := Student{
Name: "seeta1",
Age: 18,
Sid: "s20180908",
Status: 1,
}
3.3.1 findOne
mongo, err := mgo.Dial("192.168.0.91")
defer mongo.Close()
if err != nil {
return false
}
client := mongo.DB("mydb_tutorial").C("t_student")
user := Student{}
//查找sid為 s20180907
cErr := client.Find(bson.M{"sid": "s20180907"}).One(&user)
if cErr != nil {
return false
}
fmt.Println(user)
return true
執(zhí)行該段程序,會(huì)打印查找到的結(jié)果:
{seeta 17 s20180907 1}
3.3.2 findAll
查找status為1的數(shù)據(jù)
mongo, err := mgo.Dial("192.168.0.91")
defer mongo.Close()
if err != nil {
return false
}
client := mongo.DB("mydb_tutorial").C("t_student")
//每次最多輸出15條數(shù)據(jù)
iter := client.Find(bson.M{"status": 1}).Sort("_id").Skip(1).Limit(15).Iter()
var stu Student
var users Per
for iter.Next(&stu) {
users.Per = append(users.Per, stu)
}
if err := iter.Close(); err != nil {
return false
}
fmt.Println(users)
return true
執(zhí)行該段程序,會(huì)打印查找到的結(jié)果:
{[{seeta1 18 s20180908 1} {seeta 18 s20180907 1}]}
3.4 更新數(shù)據(jù)
更新數(shù)據(jù)前:
mongo, err := mgo.Dial("192.168.0.91")
defer mongo.Close()
if err != nil {
return false
}
client := mongo.DB("mydb_tutorial").C("t_student")
//只更新一條
cErr := client.Update(bson.M{"status": 1}, bson.M{"$set": bson.M{"age": 20}})
if cErr != nil {
return false
}
return true
}
執(zhí)行命令后:
只更新了一條數(shù)據(jù)的年齡
如果我們想把所有status為1的學(xué)生年齡都更新為20.
用client.UpdateAll 替換client.Update 就可以了
3.5 刪除數(shù)據(jù)
刪除數(shù)據(jù):sid為s20180907
mongo, err := mgo.Dial("192.168.0.91")
defer mongo.Close()
if err != nil {
return false
}
client := mongo.DB("mydb_tutorial").C("t_student")
//只更新一條
cErr := client.Remove(bson.M{"sid": "s20180907"})
if cErr != nil {
return false
}
return true
如果數(shù)據(jù)庫設(shè)計(jì)的時(shí)候,有兩個(gè)sid為s20180907 只會(huì)刪除一條記錄侥涵。
如果刪除所有:用client.RemoveAll 替換client.Remove
4. 其他
寫了一個(gè)gin和mgo結(jié)合的數(shù)據(jù)查詢服務(wù)demo,細(xì)節(jié)可點(diǎn)擊鏈接到github查看