package main
import (
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"log"
)
type Student struct {
//Id_ bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
Phone string `bson:"phone"`
Email string `bson:"email"`
Sex string `bson:"sex"`
}
//數(shù)據(jù)庫(kù)連接主要用到了mgo中的Dial()函數(shù),連接形式如mgo.Dial(url1,url2,url3)
func ConnecToDB() *mgo.Collection {
session, err := mgo.Dial("127.0.0.1:27017")
if err != nil {
panic(err)
}
//defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("medex").C("student")
return c
}
//插入主要用到了函數(shù) func (c *Collection) Insert(docs ...interface{}) error
func InsertToMogo() {
c := ConnecToDB()
stu1 := Student{
Name: "zhangsan",
Phone: "13480989765",
Email: "329832984@qq.com",
Sex: "F",
}
stu2 := Student{
Name: "liss",
Phone: "13980989767",
Email: "12832984@qq.com",
Sex: "M",
}
err := c.Insert(&stu1, &stu2)
if err != nil {
log.Fatal(err)
}
}
//查詢(xún)單個(gè)主要用到了func (c *Collection) Find(query interface{}) *Query函數(shù),查詢(xún)單個(gè)和多個(gè)主要用到了One()和Many()函數(shù),條件組合可以查看mongDB數(shù)據(jù)庫(kù)使用
func GetDataViaSex() {
c := ConnecToDB()
result := Student{}
err := c.Find(bson.M{"sex": "M"}).One(&result)
if err != nil {
log.Fatal(err)
}
fmt.Println("student", result)
students := make([]Student, 20)
err = c.Find(nil).All(&students)
if err != nil {
log.Fatal(err)
}
fmt.Println(students)
}
//查詢(xún)所有形如:c.Find(nil).Many(&results)
//另外,方法中也有個(gè)根據(jù)id來(lái)查詢(xún)的方法 func (c *Collection) FindId(id interface{}) *Query,
func GetDataViaId() {
id := bson.ObjectIdHex("5a66a96306d2a40a8b884049")
c := ConnecToDB()
stu := &Student{}
err := c.FindId(id).One(stu)
if err != nil {
log.Fatal(err)
}
fmt.Println(stu)
}
//更新通過(guò)函數(shù)
//*func (c *Collection) Update(selector interface{}, update interface{}) error
//*func (c *Collection) UpdateAll(selector interface{}, update interface{}) (info *ChangeInfo, err error)
//*func (c *Collection) UpdateId(id interface{}, update interface{}) error
func UpdateDBViaId() {
//id := bson.ObjectIdHex("5a66a96306d2a40a8b884049")
c := ConnecToDB()
err := c.Update(bson.M{"email": "12832984@qq.com"}, bson.M{"$set": bson.M{"name": "haha", "phone": "37848"}})
if err != nil {
log.Fatal(err)
}
}
//刪除對(duì)應(yīng)的方法
//
//func (c *Collection) Remove(selector interface{}) error]
//func (c *Collection) RemoveAll(selector interface{}) (info *ChangeInfo, err error)
//func (c *Collection) RemoveId(id interface{}) error
func RemoveFromMgo() {
c := ConnecToDB()
_, err := c.RemoveAll(bson.M{"phone": "13480989765"})
if err != nil {
log.Fatal(err)
}
}
func main() {
InsertToMogo()
GetDataViaSex()
GetDataViaId()
UpdateDBViaId()
RemoveFromMgo()
}
網(wǎng)站欄目:golang對(duì)mongodb的基本操作
瀏覽地址:http://www.chinadenli.net/article12/iphcgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、虛擬主機(jī)、Google、品牌網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)