自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Go 語言在 MongoDB 中常用查詢與修改的操作,你都會了嗎?

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

以下所有例子中結(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來進行查詢,返回的Query struct可以有附加各種條件來進行過濾。

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

條件用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。當找不到的時候用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來進行修改操作。

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(set)¨G7G2.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(push)¨G9G2.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"})

補充: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"}相當直接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)接收,當你不了解返回的數(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}
     // ...
}

查找指定字段

func (q *Query) Select(selector interface{}) *Query

上面使用Select函數(shù):

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ù)進行排序使用Sort函數(shù)

func (q *Query) Sort(fields ...string) *Query

上面就是使用sort函數(shù)的方法

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} ...]
     // ...
}
責任編輯:武曉燕 來源: Go語言圈
相關(guān)推薦

2019-05-20 16:30:36

PythonMySQL存儲

2018-04-26 16:35:44

PythonMySQL存儲

2023-06-30 09:45:00

文件讀寫操作Java

2023-11-27 11:42:46

DjangoPython

2024-08-12 11:32:12

Go語言程序

2022-01-17 07:50:37

Go代碼規(guī)范

2024-01-02 07:43:47

Go語言靜態(tài)類型

2022-09-21 07:22:39

mongodb版本docker

2023-12-24 22:30:05

LambdaJava函數(shù)

2024-05-09 08:14:09

系統(tǒng)設(shè)計語言多語言

2024-10-18 10:35:10

2022-08-29 08:05:44

Go類型JSON

2022-09-29 08:32:14

查詢語句OR

2023-03-14 23:24:27

Mongodb副本集架構(gòu)

2024-04-07 08:15:13

Go語言工具

2021-06-05 06:52:16

Kubernetes

2024-02-21 19:02:05

Go模板化方式

2024-03-18 08:06:59

JavaGo開發(fā)

2022-11-11 08:29:24

C語言中文字符代碼

2023-08-28 07:02:10

點贊
收藏

51CTO技術(shù)棧公眾號