佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Go 在 MongoDB 中常用查詢與修改的操作

Go 在 MongoDB 中常用查詢與修改的操作

熱門標(biāo)簽:網(wǎng)站排名優(yōu)化 Linux服務(wù)器 呼叫中心市場需求 服務(wù)外包 鐵路電話系統(tǒng) 百度競價排名 地方門戶網(wǎng)站 AI電銷

以下所有例子中結(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、查詢

通過func (c *Collection) Find(query interface{}) *Query來進(jìn)行查詢,返回的Query struct可以有附加各種條件來進(jìn)行過濾。

通過Query.All()可以獲得所有結(jié)果,通過Query.One()可以獲得一個結(jié)果,注意如果沒有數(shù)據(jù)或者數(shù)量超過一個,One()會報(bào)錯。

條件用bson.M{key: value},注意key必須用MongoDB中的字段名,而不是struct的字段名。

1.1、查詢所有

var users []User
c.Find(nil).All(users)

上面代碼可以把所有Users都查出來:

1.2、根據(jù)ObjectId查詢

id := "5204af979955496907000001"
objectId := bson.ObjectIdHex(id)
user := new(User)
c.Find(bson.M{"_id": objectId}).One(user)

更簡單的方式是直接用FindId()方法:

c.FindId(objectId).One(user)

注意這里沒有處理err。當(dāng)找不到的時候用One()方法會出錯。

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、修改

通過func (*Collection) Update來進(jìn)行修改操作。

func (c *Collection) Update(selector interface{}, change interface{}) error

注意修改單個或多個字段需要通過$set操作符號,否則集合會被替換。

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ù)組第三個數(shù)增加值
c.Update(
    bson.M{"_id": bson.ObjectIdHex("5204af979955496907000001")},
    bson.M{"$inc": bson.M{ "Num." + strconv.Itoa(2): 1 }})

2.3、push($push)

//從數(shù)組中增加一個元素
c.Update(
    bson.M{"_id": bson.ObjectIdHex("5204af979955496907000001")},
    bson.M{"$push": bson.M{ "interests": "Golang" }}
)

2.4、pull($pull)

//從數(shù)組中刪除一個元素
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庫操作,插入操作主要使用mongodb中Collection對象的Find方法,函數(shù)原型:

func (c *Collection) Find(query interface{}) *Query

查找的時候Find的參數(shù)都會用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接收,一般情況下都會這樣處理
     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)格式時,可以用這個先查看,然后再定義struct格式
     // 在處理mongodb組合查詢時,經(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
 
}

對數(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} ...]
     // ...
}

分頁查詢

使用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的地方開始取兩條記錄
     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} ...]
     // ...
}

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • Golang Mongodb模糊查詢的使用示例
  • 詳解Golang使用MongoDB通用操作
  • Golang對MongoDB數(shù)據(jù)庫的操作簡單封裝教程

標(biāo)簽:衡水 黃山 仙桃 銅川 蘭州 湘潭 湖南 崇左

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Go 在 MongoDB 中常用查詢與修改的操作》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    社会| 防城港市| 内丘县| 武冈市| 松潘县| 莱阳市| 博罗县| 武城县| 绵阳市| 始兴县| 宣武区| 景宁| 六枝特区| 蓬溪县| 白城市| 沂源县| 义马市| 河北区| 宿松县| 宝应县| 巴彦县| 沂源县| 汝阳县| 永德县| 新河县| 喀喇| 曲麻莱县| 平定县| 池州市| 原阳县| 七台河市| 安溪县| 万全县| 临夏市| 封丘县| 饶阳县| 泸水县| 霍林郭勒市| 西盟| 永嘉县| 高唐县|