MongoDB(Golang)查詢&修改

以下所有例子中結(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"
}

1、查詢

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

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

條件用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)找不到的時(shí)候用One()方法會出錯(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)

no in($nin)

同$in,

是否包含這個(gè)鍵($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)

$size

查詢鍵值為長度是size的數(shù)組
c.Find(bson.M{"Interests": bson.M{"$size": 3}}).All(&users)

上面就是查詢Interests數(shù)組長度為3的所有人

$all

查詢數(shù)組中包含所有值得匹配(不分順序,只看包含與否)

c.Find(bson.M{"Interests": bson.M{"$all": []string{"11","22","33"}}}).All(&users)

上面就是查詢Interests中包含11,22,33的所有人

如果數(shù)組只有一項(xiàng)內(nèi)容

c.Find(bson.M{"Interests": bson.M{"$all": []string{"11"}}}).All(&users)
c.Find(bson.M{"Interests": "11"}).All(&users)

以上結(jié)果一致

key.index
如果要查詢數(shù)組指定位置

c.Find(bson.M{"Interests.2": "33"}).All(&users)

以上就是查詢Interests的第二個(gè)元素為"33"的所有人

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

注意修改單個(gè)或多個(gè)字段需要通過$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, }}
)

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"})

這里也支持多條件,參考多條件查詢。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 目錄 查詢操作 集合查詢方法 find() 查詢內(nèi)嵌文檔 查詢操作符(內(nèi)含 數(shù)組查詢) "$gt" 、"$gte"...
    彩虹之夢閱讀 1,118評論 0 1
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,502評論 19 139
  • MongoDB常用操作 一、查詢 find方法 查詢所有的結(jié)果: select * from users;===d...
    止風(fēng)者閱讀 649評論 1 3
  • Mongodb 配置選項(xiàng) 通常在mongod.conf中 配置文件 設(shè)置了配置文件后啟動(dòng)時(shí)以自定義的配置文件啟動(dòng):...
    AkaTBS閱讀 1,144評論 0 6
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,002評論 0 23

友情鏈接更多精彩內(nèi)容