温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

golang对mongodb的基本操作

发布时间:2020-07-01 12:27:19 来源:网络 阅读:923 作者:梁十八 栏目:编程语言
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"`
}

//数据库连接主要用到了mgo中的Dial()函数,连接形式如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
}

//插入主要用到了函数 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)
    }
}

//查询单个主要用到了func (c *Collection) Find(query interface{}) *Query函数,查询单个和多个主要用到了One()和Many()函数,条件组合可以查看mongDB数据库使用
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)
}

//查询所有形如:c.Find(nil).Many(&results)
//另外,方法中也有个根据id来查询的方法 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)
}

//更新通过函数
//*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)
    }
}

//删除对应的方法
//
//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()
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI