久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

使用Golang怎么對MongoDB數(shù)據(jù)庫進(jìn)行操作

本篇文章給大家分享的是有關(guān)使用Golang怎么對MongoDB數(shù)據(jù)庫進(jìn)行操作,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),荊州企業(yè)網(wǎng)站建設(shè),荊州品牌網(wǎng)站建設(shè),網(wǎng)站定制,荊州網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,荊州網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

初始化

操作沒有用戶權(quán)限的MongoDB

var globalS *mgo.Session

func init() {
 s, err := mgo.Dial(dialInfo)
 if err != nil {
 log.Fatalf("Create Session: %s\n", err)
 }
 globalS = s
}

如果MongoDB設(shè)置了用戶權(quán)限需要使用下面的方法操作

func init() {
 dialInfo := &mgo.DialInfo{
 Addrs: []string{dbhost}, //數(shù)據(jù)庫地址 dbhost: mongodb://user@123456:127.0.0.1:27017
 Timeout: timeout,  // 連接超時時間 timeout: 60 * time.Second
 Source: authdb,  // 設(shè)置權(quán)限的數(shù)據(jù)庫 authdb: admin
 Username: authuser,  // 設(shè)置的用戶名 authuser: user
 Password: authpass, // 設(shè)置的密碼 authpass: 123456
 PoolLimit: poollimit, // 連接池的數(shù)量 poollimit: 100
 }

 s, err := mgo.DialWithInfo(dialInfo)
 if err != nil {
 log.Fatalf("Create Session: %s\n", err)
 }
 globalS = s
}

連接具體的數(shù)據(jù)和文檔

每一次操作都copy一份 Session,避免每次創(chuàng)建Session,導(dǎo)致連接數(shù)量超過設(shè)置的最大值

獲取文檔對象 c := Session.DB(db).C(collection)

func connect(db, collection string) (*mgo.Session, *mgo.Collection) {
 ms := globalS.Copy()
 c := ms.DB(db).C(collection)
 ms.SetMode(mgo.Monotonic, true)
 return ms, c
}

插入數(shù)據(jù)

每次操作之后都要主動關(guān)閉 Session defer Session.Close()

db:操作的數(shù)據(jù)庫

collection:操作的文檔(表)

doc:要插入的數(shù)據(jù)

func Insert(db, collection string, doc interface{}) error {
 ms, c := connect(db, collection)
 defer ms.Close()

 return c.Insert(doc)
}

// test
data := &Data{
 Id: bson.NewObjectId().Hex(),
 Title: "標(biāo)題",
 Des: "博客描述信息",
 Content: "博客的內(nèi)容信息",
 Img: "/upload/otherpic64/8679037-67456031925afca6.png%7CimageView2/2/w/700",
 Date: time.Now(),
}

err := db.Insert("Test", "TestModel", data)

查詢數(shù)據(jù)

db:操作的數(shù)據(jù)庫

collection:操作的文檔(表)

query:查詢條件

selector:需要過濾的數(shù)據(jù)(projection)

result:查詢到的結(jié)果

func FindOne(db, collection string, query, selector, result interface{}) error {
 ms, c := connect(db, collection)
 defer ms.Close()

 return c.Find(query).Select(selector).One(result)
}

func FindAll(db, collection string, query, selector, result interface{}) error {
 ms, c := connect(db, collection)
 defer ms.Close()

 return c.Find(query).Select(selector).All(result)
}

//test 查詢title="標(biāo)題",并且返回結(jié)果中去除`_id`字段
var result Data
err = db.FindOne(database, collection, bson.M{"title": "標(biāo)題"}, bson.M{"_id":0}, &result)

更新數(shù)據(jù)

db:操作的數(shù)據(jù)庫

collection:操作的文檔(表)

selector:更新條件

update:更新的操作

func Update(db, collection string, selector, update interface{}) error {
 ms, c := connect(db, collection)
 defer ms.Close()

 return c.Update(selector, update)
}

//更新,如果不存在就插入一個新的數(shù)據(jù) `upsert:true`
func Upsert(db, collection string, selector, update interface{}) error {
 ms, c := connect(db, collection)
 defer ms.Close()

 _, err := c.Upsert(selector, update)
 return err
}

// `multi:true`
func UpdateAll(db, collection string, selector, update interface{}) error {
 ms, c := connect(db, collection)
 defer ms.Close()

 _, err := c.UpdateAll(selector, update)
 return err
}

//test
err = db.Update(database, collection, bson.M{"_id": "5b3c30639d5e3e24b8786540"}, bson.M{"$set": bson.M{"title": "更新標(biāo)題"}})

刪除數(shù)據(jù)

db:操作的數(shù)據(jù)庫

collection:操作的文檔(表)

selector:刪除條件

func Remove(db, collection string, selector interface{}) error {
 ms, c := connect(db, collection)
 defer ms.Close()

 return c.Remove(selector)
}

func RemoveAll(db, collection string, selector interface{}) error {
 ms, c := connect(db, collection)
 defer ms.Close()

 _, err := c.RemoveAll(selector)
 return err
}

//test
err = db.Remove(database,collection,bson.M{"_id":"5b3c30639d5e3e24b8786540"})

分頁查詢

db:操作的數(shù)據(jù)庫

collection:操作的文檔(表)

page:當(dāng)前頁面

limit:每頁的數(shù)量值

query:查詢條件

selector:需要過濾的數(shù)據(jù)(projection)

result:查詢到的結(jié)果

func FindPage(db, collection string, page, limit int, query, selector, result interface{}) error {
 ms, c := connect(db, collection)
 defer ms.Close()

 return c.Find(query).Select(selector).Skip(page * limit).Limit(limit).All(result)
}

其他操作

func IsEmpty(db, collection string) bool {
 ms, c := connect(db, collection)
 defer ms.Close()
 count, err := c.Count()
 if err != nil {
 log.Fatal(err)
 }
 return count == 0
}

func Count(db, collection string, query interface{}) (int, error) {
 ms, c := connect(db, collection)
 defer ms.Close()
 return c.Find(query).Count()
}

以上就是使用Golang怎么對MongoDB數(shù)據(jù)庫進(jìn)行操作,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

名稱欄目:使用Golang怎么對MongoDB數(shù)據(jù)庫進(jìn)行操作
網(wǎng)站地址:http://sd-ha.com/article20/jgijjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、App開發(fā)、微信公眾號網(wǎng)站改版、動態(tài)網(wǎng)站、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化