golang基础学习-MongoDB使用1

1,598 阅读1分钟

1.系统环境

  • Golang:go version go1.13.4 darwin/amd64
  • OS:MacOS
  • MongoDB: version: 4.2

1.1 安装mongodb

提前安装了docker,使用docker安装的mongodb。

docker pull mongo:v4.2

  • mongodb3.2+: 使用$lookup可以支持连表查询
  • mongodb4.0+: 支持事务
后续会编写一篇介绍这两种特性的文章。

2.Golang使用MongoDB

使用:gopkg.in/mgo.v2

  获取包:go get gopkg.in/mgo.v2
  引入:import "gopkg.in/mgo.v2"

mgo简介

3.简单使用MongoDB

3.1 数据设计

3.1.1 数据库设计:

数据库名:mydb_tutorial 集合名: t_student

数据集合:t_student字段说明

字段 类型 说明
name string 姓名
age int 年龄
sid string 学号
status int 状态:1正常,9,删除

3.1.2结构体设计:

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简介

func Dial(url string) (*Session, error)

官方简介:Dial establishes a new session to the cluster identified by the given seed server(s).

3.2 插入数据


	mongo, err := mgo.Dial("127.0.0.1") // 建立连接 

	defer mongo.Close()

	if err != nil {
		return false
	}

	client := mongo.DB("mydb_tutorial").C("t_student")  //选择数据库和集合

    //创建数据
	data := Student{
		Name:   "seeta",
		Age:    18,
		Sid:    "s20180907",
		Status: 1,
	}

    //插入数据
	cErr := client.Insert(&data)

	if cErr != nil {
		return false
	}
	return true

执行该段程序,MongoDB会出现一条记录:

使用mongodb官网的mongodb-compass查看数据信息。

3.3 查找数据

在3.2插入数据的基础上,我们再插入一条数据:

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

执行该段程序,会打印查找到的结果:

{seeta 17 s20180907 1}

3.3.2 findAll

查找status为1的数据

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条数据
	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

执行该段程序,会打印查找到的结果:

{[{seeta1 18 s20180908 1} {seeta 18 s20180907 1}]}

3.4 更新数据

更新数据前:

	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
}

执行命令后:

只更新了一条数据的年龄

如果我们想把所有status为1的学生年龄都更新为20.

用client.UpdateAll 替换client.Update 就可以了

3.5 删除数据

删除数据: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

如果数据库设计的时候,有两个sid为s20180907 只会删除一条记录。 如果删除所有:用client.RemoveAll 替换client.Remove