包
go get -u gopkg.in/mgo.v2
文档位置
https://pkg.go.dev/gopkg.in/mgo.v2#Collection.UpdateAll
首先解释一下bson.M和bson.D的用法
import (
"gopkg.in/mgo.v2/bson"
)
- D:一个BSON文档,这个类型应该被用在顺序比较重要的场合(言外之意就是有序的),比如说mongodb的命令。
- M:一个无序的map。它除了无序之外和D是一样的(可以理解为map和bson是可以转换)。
底层代码
// M is a convenient alias for a map[string]interface{} map, useful for
// dealing with BSON in a native way. For instance:
//
// bson.M{"a": 1, "b": true}
//
// There's no special handling for this type in addition to what's done anyway
// for an equivalent map type. Elements in the map will be dumped in an
// undefined ordered. See also the bson.D type for an ordered alternative.
type M map[string]interface{}
// D represents a BSON document containing ordered elements. For example:
//
// bson.D{{"a", 1}, {"b", true}}
//
// In some situations, such as when creating indexes for MongoDB, the order in
// which the elements are defined is important. If the order is not important,
// using a map is generally more comfortable. See bson.M and bson.RawD.
type D []DocElem
使用的例子:
通过bson.D来添加集合,也可以通过bson.M添加
a := bson.D{{"name", "世界"}, {"hs", "哈哈哈"}}
// a := bson.M{"name":"我是m", "hs":"看到来没"}
c := session.DB("more").C("m_test")
c.Insert(a)
开始curd 操作
引入包
import (
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"time"
)
创建结构体,一个是查询单条结构体,这个结构体必须要和数据库中的字段一样,不然是读取不了数据的,并且读取是json数据
type Test struct {
Name string `json:"name"`
Hs string `json:"hs"`
Number int64 `json:"number"`
}
数据库结构如下
创建mongodb会话
dialInfo := &mgo.DialInfo{
Addrs: []string{"localhost:端口"},
Timeout: time.Second * 1, // 超时时间
Database: "******", // 要访问的集合(对应mysql的数据库)
Username: "******",// 用户名
Password: "******",// 密码
}
session, err := mgo.DialWithInfo(dialInfo)// DialWithInfo建立一个到由mgo.DialInfo标识的集群的新会话。
if nil != err {
panic(err)
}
新增
//通过bson.D来添加集合,也可以通过bson.M添加
a := bson.D{{"name", "世界"}, {"hs", "哈哈哈"}}
//a := bson.M{"name":"我是m", "hs":"看到来没"}
c := session.DB("more").C("m_test")
c.Insert(a)
查询所有
var t []Test 是前面定义的结构体
var t []Test
c := session.DB("more").C("m_test")// DB 数据库,C 集合
// 如果你想查所有,不需要用条件的,bson.M{"name":"Armies"} 换成 bson.M{}
c.Find(bson.M{}).All(&t)
fmt.Println(t)// [{Army of Ants as} {Army Ants as} {Ant Man as} {Armies as}]
defer session.Close()
查询单条
var t Test
c := session.DB("more").C("m_test")
// 如果你想查所有,不需要用条件的,bson.M{"name":"Armies"} 换成 bson.M{}
c.Find(bson.M{}).One(&t)
fmt.Println(t)// {Army of Ants as}
defer session.Close()
条件查询
不等于!=($ne)
c.Find(bson.M{"name": bson.M{"$ne": "Tom"}}).All(&users)
大于>($gt)
c.Find(bson.M{"age": bson.M{"$gt": 10}}).All(&users)
小于<($lt)
c.Find(bson.M{"age": bson.M{"$lt": 10}}).All(&users)
大于等于>=($gte)
c.Find(bson.M{"age": bson.M{"$gte": 10}}).All(&users)
小于等于<=($lte)
c.Find(bson.M{"age": bson.M{"$lte": 10}}).All(&users)
in($in)
c.Find(bson.M{"name": bson.M{"$in": []string{"Tom", "Jerry"}}}).All(&users)
no in(nin)同in
是否包含这个键($exists)
c.Find(bson.M{"name": bson.M{"$exists": true}}).All(&users)
查询键值为null的字段
c.Find(bson.M{"name": bson.M{"$in":[]interface{}{null}, "$exists": true}}).All(&users)
正则匹配($regex)
c.Find(bson.M{"name": bson.M{"$regex": "^[0-9]+"}}).All(&users)
$all查询所有
c.Find(bson.M{"name": bson.M{"$all": []int{10,11,12}}}).All(&users
$or
c.Find(bson.M{"$or": []bson.M{bson.M{"age": 11}, bson.M{"sex": 1}}}).All(&users)
例子
var t []Test
c := session.DB("more").C("m_test")
c.Find(bson.M{"number":bson.M{"$ne":1111}}).All(&t)
fmt.Println(t)
修改
// 3-1 修改一条
c := session.DB("more").C("m_test")
c.Update(bson.M{"hs":"asdi"}, bson.M{"$set":bson.M{"hs":"update2"}})
// 3-2 修改多条
//c := session.DB("more").C("m_test")
//c.UpdateAll(bson.M{"hs":"世界"}, bson.M{"$set": bson.M{"hs": "asdi", "name":"哈哈哈说"}})
删除
//4、删除
//4-1 删除一条
c := session.DB("more").C("m_test")
c.Remove(bson.M{"name":"我是m"})
//4-2 删除多条
c := session.DB("more").C("m_test")
c.RemoveAll(bson.M{"name":"我是m"})
完整代码文件
package service
import (
"gopkg.in/mgo.v2"
"time"
)
type Test struct {
Name string `json:"name"`
Hs string `json:"hs"`
Number int64 `json:"number"`
}
func MongodbTest() {
dialInfo := &mgo.DialInfo{
Addrs: []string{"localhost:27017"},
Timeout: time.Second * 1,
Database: "more",
Username: "",
Password: "",
}
session, err := mgo.DialWithInfo(dialInfo)
if nil != err {
panic(err)
}
defer session.Close()
// 1、搜索所有
//var t []Test
//c := session.DB("more").C("m_test")
如果你想查所有,不需要用条件的,bson.M{"name":"Armies"} 换成 bson.M{}
//c.Find(bson.M{}).All(&t)
//fmt.Println(t)// [{Army of Ants as} {Army Ants as} {Ant Man as} {Armies as}]
//defer session.Close()
// 2、搜索单条
//var t Test
//c := session.DB("more").C("m_test")
如果你想查所有,不需要用条件的,bson.M{"name":"Armies"} 换成 bson.M{}
//c.Find(bson.M{}).One(&t)
//fmt.Println(t)// {Army of Ants as}
// 3、条件搜索, $ne 不等于
//c := session.DB("more").C("m_test")
//c.Find(bson.M{"number":bson.M{"$ne":1111}}).All(&t)
//fmt.Println(t)
// 2、添加
//通过bson.D来添加集合,也可以通过bson.M添加
//a := bson.D{{"name", "世界"}, {"hs", "哈哈哈"}, {"number", 1111}}
a := bson.M{"name":"我是m", "hs":"看到来没"}
//c := session.DB("more").C("m_test")
//c.Insert(a)
// 3、修改
// 3-1 修改一条
//c := session.DB("more").C("m_test")
//c.Update(bson.M{"hs":"asdi"}, bson.M{"$set":bson.M{"hs":"update2"}})
// 3-2 修改多条
//c := session.DB("more").C("m_test")
//c.UpdateAll(bson.M{"hs":"世界"}, bson.M{"$set": bson.M{"hs": "asdi", "name":"哈哈哈说"}})
//4、删除
//4-1 删除一条
//c := session.DB("more").C("m_test")
//c.Remove(bson.M{"name":"我是m"})
// 4-2 删除多条
//c := session.DB("more").C("m_test")
//c.RemoveAll(bson.M{"name":"我是m"})
}