ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

知识点 go操作mongodb

2019-08-17 16:02:28  阅读:204  来源: 互联网

标签:知识点 err mongodb Collection interface func go bson id


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)
}
}
通过可视化工具可以看到我插入的数据
{
"_id" : ObjectId("5a66a96306d2a40a8b884049"),
"name" : "zhangsan",
"phone" : "13480989765",
"email" : "329832984@qq.com",
"sex" : "F"
}

{
"_id" : ObjectId("5a66a96306d2a40a8b88404a"),
"name" : "liss",
"phone" : "13980989767",
"email" : "12832984@qq.com",
"sex" : "M"
}
三、查询
查询单个主要用到了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)
}
}

标签:知识点,err,mongodb,Collection,interface,func,go,bson,id
来源: https://www.cnblogs.com/chu-12345/p/11368945.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有