Go 在 MongoDB 中常用查詢與修改的操作
以下所有例子中結(jié)構(gòu)定義如下:
type User struct {
Id_ bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
Age int `bson:"age"`
JoinedAt time.Time `bson:"joined_at"`
Interests []string `bson:"interests"`
Num []int `bson:"num"`
}
1、查詢
通過(guò)func (c *Collection) Find(query interface{}) *Query來(lái)進(jìn)行查詢,返回的Query struct可以有附加各種條件來(lái)進(jìn)行過(guò)濾。
通過(guò)Query.All()可以獲得所有結(jié)果,通過(guò)Query.One()可以獲得一個(gè)結(jié)果,注意如果沒(méi)有數(shù)據(jù)或者數(shù)量超過(guò)一個(gè),One()會(huì)報(bào)錯(cuò)。
條件用bson.M{key: value},注意key必須用MongoDB中的字段名,而不是struct的字段名。
1.1、查詢所有
var users []User c.Find(nil).All(&users)
上面代碼可以把所有Users都查出來(lái):
1.2、根據(jù)ObjectId查詢
id := "5204af979955496907000001"
objectId := bson.ObjectIdHex(id)
user := new(User)
c.Find(bson.M{"_id": objectId}).One(&user)
更簡(jiǎn)單的方式是直接用FindId()方法:
c.FindId(objectId).One(&user)
注意這里沒(méi)有處理err。當(dāng)找不到的時(shí)候用One()方法會(huì)出錯(cuò)。
1.3、單條件查詢
=($eq)
c.Find(bson.M{"name": "Jimmy Kuu"}).All(&users)
!=($ne)
c.Find(bson.M{"name": bson.M{"$ne": "Jimmy Kuu"}}).All(&users)
>($gt)
c.Find(bson.M{"age": bson.M{"$gt": 32}}).All(&users)
<($lt)
c.Find(bson.M{"age": bson.M{"$lt": 32}}).All(&users)
>=($gte)
c.Find(bson.M{"age": bson.M{"$gte": 33}}).All(&users)
<=($lte)
c.Find(bson.M{"age": bson.M{"$lte": 31}}).All(&users)
in($in)
c.Find(bson.M{"name": bson.M{"$in": []string{"Jimmy Kuu", "Tracy Yu"}}}).All(&users)
1.4、多條件查詢
and($and)
c.Find(bson.M{"name": "Jimmy Kuu", "age": 33}).All(&users)
or($or)
c.Find(bson.M{"$or": []bson.M{bson.M{"name": "Jimmy Kuu"}, bson.M{"age": 31}}}).All(&users)
2、修改
通過(guò)func (*Collection) Update來(lái)進(jìn)行修改操作。
func (c *Collection) Update(selector interface{}, change interface{}) error
注意修改單個(gè)或多個(gè)字段需要通過(guò)$set操作符號(hào),否則集合會(huì)被替換。
2.1、($set)
//修改字段的值
c.Update(
bson.M{"_id": bson.ObjectIdHex("5204af979955496907000001")},
bson.M{"$set": bson.M{ "name": "Jimmy Gu", "age": 34 }}
)
2.2、inc($inc)
//字段增加值
c.Update(
bson.M{"_id": bson.ObjectIdHex("5204af979955496907000001")},
bson.M{"$inc": bson.M{ "age": -1 }}
)
//字段Num數(shù)組第三個(gè)數(shù)增加值
c.Update(
bson.M{"_id": bson.ObjectIdHex("5204af979955496907000001")},
bson.M{"$inc": bson.M{ "Num." + strconv.Itoa(2): 1 }})
2.3、push($push)
//從數(shù)組中增加一個(gè)元素
c.Update(
bson.M{"_id": bson.ObjectIdHex("5204af979955496907000001")},
bson.M{"$push": bson.M{ "interests": "Golang" }}
)
2.4、pull($pull)
//從數(shù)組中刪除一個(gè)元素
c.Update(
bson.M{"_id": bson.ObjectIdHex("5204af979955496907000001")},
bson.M{"$pull": bson.M{ "interests": "Golang" }}
)
2.5、刪除
c.Remove(bson.M{"name": "Jimmy Kuu"})
補(bǔ)充:golang mongodb查找find demo
使用gopkg.in/mgo.v2庫(kù)操作,插入操作主要使用mongodb中Collection對(duì)象的Find方法,函數(shù)原型:
func (c *Collection) Find(query interface{}) *Query
查找的時(shí)候Find的參數(shù)都會(huì)用bson.M類型
type M map[string]interface{}
例如:bson.M{"name": "Tom"}相當(dāng)直接mongodb的查詢條件{"name": "Tom"}
統(tǒng)一封裝下getDB方法
package main
import (
"fmt"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// get mongodb db
func getDB() *mgo.Database {
session, err := mgo.Dial( "172.16.27.134:10001" )
if err != nil {
panic(err)
}
session.SetMode(mgo.Monotonic, true)
db := session.DB( "test" )
return db
}
查找單條記錄
func findOne() {
db := getDB()
c := db.C( "user" )
// 用struct接收,一般情況下都會(huì)這樣處理
type User struct {
Name string "bson:`name`"
Age int "bson:`age`"
}
user := User{}
err := c.Find(bson.M{ "name" : "Tom" }).One(&user)
if err != nil {
panic(err)
}
fmt.Println(user)
// output: {Tom 20}
// 用bson.M結(jié)構(gòu)接收,當(dāng)你不了解返回的數(shù)據(jù)結(jié)構(gòu)格式時(shí),可以用這個(gè)先查看,然后再定義struct格式
// 在處理mongodb組合查詢時(shí),經(jīng)常這么干
result := bson.M{}
err = c.Find(nil).One(&result)
if err != nil {
panic(err)
}
fmt.Println(result)
// output: map[_id:ObjectIdHex("56fdce98189df8759fd61e5b") name:Tom age:20]
}
查找多條記錄
func findMuit() {
db := getDB()
c := db.C( "user" )
// 使用All方法,一次性消耗較多內(nèi)存,如果數(shù)據(jù)較多,可以考慮使用迭代器
type User struct {
Id bson.ObjectId `bson: "_id,omitempty" `
Name string "bson:`name`"
Age int "bson:`age`"
}
var users []User
err := c.Find(nil).All(&users)
if err != nil {
panic(err)
}
fmt.Println(users)
// output: [{ObjectIdHex("56fdce98189df8759fd61e5b") Tom 20}...]
// 使用迭代器獲取數(shù)據(jù)可以避免一次占用較大內(nèi)存
var user User
iter := c.Find(nil).Iter()
for iter.Next(&user) {
fmt.Println(user)
}
// output:
// {ObjectIdHex("56fdce98189df8759fd61e5b") Tom 20}
// {ObjectIdHex("56fdce98189df8759fd61e5c") Tom 20}
// ...
}
查找指定字段
主要使用Select函數(shù):
func (q *Query) Select(selector interface{}) *Query
func findField() {
db := getDB()
c := db.C( "user" )
// 只讀取name字段
type User struct {
Name string "bson:`name`"
}
var users []User
err := c.Find(bson.M{}).Select(bson.M{ "name" : 1 }).All(&users)
if err != nil {
panic(err)
}
fmt.Println(users)
// output: [{Tom} {Tom} {Anny}...]
// 只排除_id字段
type User2 struct {
Name string "bson:`name`"
Age int "bson:`age`"
}
var users2 []User2
err = c.Find(bson.M{}).Select(bson.M{ "_id" : 0 }).All(&users2)
if err != nil {
panic(err)
}
fmt.Println(users2)
// output: [{Tom 20} {Tom 20} {Anny 28}...]
}
查詢嵌套格式數(shù)據(jù)
func findNesting() {
db := getDB()
c := db.C( "user" )
// 使用嵌套的struct接收數(shù)據(jù)
type User struct {
Name string "bson:`name`"
Age int "bson:`age`"
Toys []struct {
Name string "bson:`name`"
}
}
var users User
// 只查詢toys字段存在的
err := c.Find(bson.M{ "toys" : bson.M{ "$exists" : true}}).One(&users)
if err != nil {
panic(err)
}
fmt.Println(users)
// output: {Tom 20 [{dog}]}
}
查找數(shù)據(jù)總數(shù)
func count() {
db := getDB()
c := db.C( "user" )
// 查找表總數(shù)
count, err := c.Count()
if err != nil {
panic(err)
}
fmt.Println(count)
// output: 8
// 結(jié)合find條件查找
count, err = c.Find(bson.M{ "name" : "Tom" }).Count()
if err != nil {
panic(err)
}
fmt.Println(count)
// output: 6
}
對(duì)數(shù)據(jù)進(jìn)行排序
使用Sort函數(shù)
func (q *Query) Sort(fields ...string) *Query
func findSort() {
db := getDB()
c := db.C( "user" )
type User struct {
Id bson.ObjectId `bson: "_id,omitempty" `
Name string "bson:`name`"
Age int "bson:`age`"
}
var users []User
// 按照age字段降序排列,如果升序去掉橫線"-"就可以了
err := c.Find(nil).Sort( "-age" ).All(&users)
if err != nil {
panic(err)
}
fmt.Println(users)
// output:
// [{ObjectIdHex("56fdce98189df8759fd61e5d") Anny 28} ...]
// ...
}
分頁(yè)查詢
使用Skip函數(shù)和Limit函數(shù)
func (q *Query) Skip(n int) *Query func (q *Query) Limit(n int) *Query
func findPage() {
db := getDB()
c := db.C( "user" )
type User struct {
Id bson.ObjectId `bson: "_id,omitempty" `
Name string "bson:`name`"
Age int "bson:`age`"
}
var users []User
// 表示從偏移位置為2的地方開(kāi)始取兩條記錄
err := c.Find(nil).Sort( "-age" ).Skip( 2 ).Limit( 2 ).All(&users)
if err != nil {
panic(err)
}
fmt.Println(users)
// output:
// [{ObjectIdHex("56fdce98189df8759fd61e5d") Anny 20} ...]
// ...
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Hugo 游樂(lè)場(chǎng)內(nèi)容初始化示例詳解
這篇文章主要為大家介紹了Hugo 游樂(lè)場(chǎng)內(nèi)容初始化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
golang遍歷時(shí)修改被遍歷對(duì)象的示例詳解
這篇文章主要介紹了golang遍歷時(shí)修改被遍歷對(duì)象的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Go語(yǔ)言普通指針unsafe.Pointer?uintpt之間的關(guān)系及指針運(yùn)算
這篇文章主要為大家介紹了Go語(yǔ)言普通指針unsafe.Pointer?uintpt之間的關(guān)系及指針運(yùn)算示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Golang設(shè)計(jì)模式之責(zé)任鏈模式講解和代碼示例
責(zé)任鏈?zhǔn)且环N行為設(shè)計(jì)模式, 允許你將請(qǐng)求沿著處理者鏈進(jìn)行發(fā)送, 直至其中一個(gè)處理者對(duì)其進(jìn)行處理,本文就詳細(xì)給大家介紹一下Golang 責(zé)任鏈模式,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-06-06
golang Iris運(yùn)行多個(gè)應(yīng)用的實(shí)現(xiàn)
本文主要介紹了golang Iris運(yùn)行多個(gè)應(yīng)用的實(shí)現(xiàn),在Iris里面,提供了一種方式可以讓我們同時(shí)運(yùn)行多個(gè)應(yīng)用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
go語(yǔ)言切片slice使用細(xì)節(jié)和注意事項(xiàng)整理大全
這篇文章主要給大家介紹了關(guān)于go語(yǔ)言切片slice使用細(xì)節(jié)和注意事項(xiàng)整理的相關(guān)資料,需要的朋友可以參考下2024-05-05

