网站首页 返回列表 像“草根”一样,紧贴着地面,低调的存在,冬去春来,枯荣无恙。

Go语言开发中MongoDB数据库

2020-06-10 02:56:40 admin 697

伴随着移动端的兴起,Nosql数据库以其分布式设计和高性能等特点得到了广泛的应该用,下面将介绍下Nosql中的mongoDB在Go语言中的应用,在开发前,有必要了解下基础知识,mongo数据库,MongoDB在Go语言开发接口文档
在开发前,导入开发需要用到的类库
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),具体代码如下:

__

  1. func ConnecToDB() *mgo.Collection {
  2. session, err := mgo.Dial("127.0.0.1:27017")
  3. if err != nil {
  4. panic(err)
  5. }
  6. //defer session.Close()
  7. session.SetMode(mgo.Monotonic, true)
  8. c := session.DB("medex").C("student")
  9. return c
  10. }
二、插入

插入主要用到了函数 func (c *Collection) Insert(docs …interface{}) error
下面是我插入的两条记录

__

  1. func InsertToMogo() {
  2. c := ConnecToDB()
  3. stu1 := Student{
  4. Name: "zhangsan",
  5. Phone: "13480989765",
  6. Email: "329832984@qq.com",
  7. Sex: "F",
  8. }
  9. stu2 := Student{
  10. Name: "liss",
  11. Phone: "13980989767",
  12. Email: "12832984@qq.com",
  13. Sex: "M",
  14. }
  15. err := c.Insert(&stu1, &stu2)
  16. if err != nil {
  17. log.Fatal(err)
  18. }
  19. }

通过可视化工具可以看到我插入的数据

__

  1. {
  2. "_id" : ObjectId("5a66a96306d2a40a8b884049"),
  3. "name" : "zhangsan",
  4. "phone" : "13480989765",
  5. "email" : "329832984@qq.com",
  6. "sex" : "F"
  7. }
  8. {
  9. "_id" : ObjectId("5a66a96306d2a40a8b88404a"),
  10. "name" : "liss",
  11. "phone" : "13980989767",
  12. "email" : "12832984@qq.com",
  13. "sex" : "M"
  14. }
三、查询

查询单个主要用到了func (c Collection) Find(query interface{}) Query函数,查询单个和多个主要用到了One()和Many()函数,条件组合可以查看mongDB数据库使用。

__

  1. func GetDataViaSex() {
  2. c := ConnecToDB()
  3. result := Student{}
  4. err := c.Find(bson.M{"sex": "M"}).One(&result)
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. fmt.Println("student", result)
  9. students := make([]Student, 20)
  10. err = c.Find(nil).All(&students)
  11. if err != nil {
  12. log.Fatal(err)
  13. }
  14. fmt.Println(students)
  15. }

查询所有形如:c.Find(nil).Many(&results)
另外,方法中也有个根据id来查询的方法 func (c Collection) FindId(id interface{}) Query,

__

  1. func GetDataViaId() {
  2. id := bson.ObjectIdHex("5a66a96306d2a40a8b884049")
  3. c := ConnecToDB()
  4. stu := &Student{}
  5. err := c.FindId(id).One(stu)
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. fmt.Println(stu)
  10. }
三、更新

更新通过函数
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

__

  1. func UpdateDBViaId() {
  2. //id := bson.ObjectIdHex("5a66a96306d2a40a8b884049")
  3. c := ConnecToDB()
  4. err := c.Update(bson.M{"email": "12832984@qq.com"}, bson.M{"$set": bson.M{"name": "haha", "phone": "37848"}})
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. }
四、删除

删除对应的方法

  • func (c *Collection) Remove(selector interface{}) error]
  • func (c Collection) RemoveAll(selector interface{}) (info ChangeInfo, err error)
  • func (c *Collection) RemoveId(id interface{}) error

__

  1. func RemoveFromMgo() {
  2. c := ConnecToDB()
  3. _, err := c.RemoveAll(bson.M{"phone": "13480989765"})
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. }
写在最后

介绍了一篇mysql和一篇MongoDB在在Go语言开发中的使用,主要是为了学习下关系型数据库和NoSql数据库在开发中的使用。

转载文章,原文链接: Go语言开发中MongoDB数据库

关键字词goMongoDB

分享到:

如需留言,请 登录,没有账号?请 注册

0 条评论 0 人参与

顶部 底部