伴随着移动端的兴起,Nosql数据库以其分布式设计和高性能等特点得到了广泛的应该用,下面将介绍下Nosql中的mongoDB在Go语言中的应用,在开发前,有必要了解下基础知识
在开发前,导入开发需要用到的类库
1import ( 2"fmt" 3"gopkg.in/mgo.v2" 4"gopkg.in/mgo.v2/bson" 5"log" 6)
在示例中用到的结构有:
1type Student struct { 2Id_ bson.ObjectId bson:"_id" 3Name string bson:"name" 4Phone string bson:"phone" 5Email string bson:"email" 6Sex string bson:"sex" 7}
一、数据库连接
数据库连接主要用到了mgo中的Dial()函数,连接形式如mgo.Dial(url1,url2,url3),具体代码如下:
1func 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
下面是我插入的两条记录
1func 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{ 10 "_id" : ObjectId("5a66a96306d2a40a8b88404a"), 11 "name" : "liss", 12 "phone" : "13980989767", 13 "email" : "12832984@qq.com", 14 "sex" : "M" 15}
三、查询
查询单个主要用到了func (c *Collection) Find(query interface{}) *Query函数,查询单个和多个主要用到了One()和Many()函数,条件组合可以查看mongDB数据库使用。
1func 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 16}
查询所有形如:c.Find(nil).Many(&results)
另外,方法中也有个根据id来查询的方法 func (c *Collection) FindId(id interface{}) *Query,
1func 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}
三、更新
更新通过函数
1*func (c *Collection) Update(selector interface{}, update interface{}) error 2*func (c *Collection) UpdateAll(selector interface{}, update interface{}) (info *ChangeInfo, err error) 3*func (c *Collection) UpdateId(id interface{}, update interface{}) error 4 5func UpdateDBViaId() { 6 //id := bson.ObjectIdHex("5a66a96306d2a40a8b884049") 7 c := ConnecToDB() 8 err := c.Update(bson.M{"email": "12832984@qq.com"}, bson.M{"$set": bson.M{"name": "haha", "phone": "37848"}}) 9 if err != nil { 10 log.Fatal(err) 11 } 12}
四、删除
删除对应的方法
1func (c *Collection) Remove(selector interface{}) error] 2func (c *Collection) RemoveAll(selector interface{}) (info *ChangeInfo, err error) 3func (c *Collection) RemoveId(id interface{}) error 4func RemoveFromMgo() { 5 c := ConnecToDB() 6 _, err := c.RemoveAll(bson.M{"phone": "13480989765"}) 7 if err != nil { 8 log.Fatal(err) 9 } 10}