1. System environment
- Golang:go version go1.10.3 Darwin/amd64
- Os:macos
- mongobd:version:3.4.4
2.Golang using MongoDB
Use: Gopkg.in/mgo.v2
获取包:go get gopkg.in/mgo.v2 引入:import "gopkg.in/mgo.v2"
About MgO
3. Simply use MongoDB
3.1 Data design
3.1.1 Database design:
Database name: mydb_tutorial
Collection Name: t_student
Data collection: T_student field description
| Field |
type |
Description |
| Name |
String |
Name |
| Age |
Int |
Age |
| Sid |
String |
School Number |
| Status |
Int |
Status: 1 Normal, 9, delete |
3.1.2 Structure Design:
type Student struct { Name string `bson: "name"` Age int `bson: "age"` Sid string `bson: "sid"` Status int `bson: "status"`}type Per struct { Per []Student}
About MgO
func Dial(url string) (*Session, error)官方简介:Dial establishes a new session to the cluster identified by the given seed server(s).
3.2 Inserting data
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
The implementation of the program, MongoDB will appear a record:
3.3 Finding data
on the basis of inserting data in 3.2, we insert another piece of data:
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
When you execute the program, the results you find are printed:
{Seeta s20180907 1}
3.3.2 FindAll
Find data with 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
When you execute the program, the results you find are printed:
{[{seeta1 s20180908 1} {Seeta s20180907 1}]}
3.4 Updating data
Before updating the data:
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}
After executing the command:
Age of only one piece of data updated
If we want to update the age of all students with status 1 to 20.
with the client. UpdateAll Replace the client. Update is available.
3.5 Deleting data
Delete data: SID is 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
If the database is designed, there are two SIDs for s20180907 and only one record is deleted.
If you delete all: With client. RemoveAll Replace the client. Remove
4. Other
Wrote a gin and MgO combined data Query service demo, details can be clicked linked to GitHub view