十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
如何在mongodb中使用golang驅(qū)動(dòng)?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
使用教程如下:
導(dǎo)入
go get github.com/mongodb/mongo-go-driver/mongo
鏈接mongo服務(wù)
if client, err = mongo.Connect(getContext(), url); err != nil { checkErr(err) }
判斷服務(wù)是否可用
if err = client.Ping(getContext(), readpref.Primary()); err != nil { checkErr(err) }
選擇數(shù)據(jù)庫和集合
collection = client.Database("testing_base").Collection("howie")
刪除這個(gè)集合
collection.Drop(getContext())
插入一條數(shù)據(jù)
if insertOneRes, err = collection.InsertOne(getContext(), howieArray[0]); err != nil { checkErr(err) } fmt.Printf("InsertOne插入的消息ID:%v\n", insertOneRes.InsertedID)
批量插入數(shù)據(jù)
if insertManyRes, err = collection.InsertMany(getContext(), howieArray); err != nil { checkErr(err) } fmt.Printf("InsertMany插入的消息ID:%v\n", insertManyRes.InsertedIDs)
查詢單條數(shù)據(jù)
if err = collection.FindOne(getContext(), bson.D{{"name", "howie_2"}, {"age", 11}}).Decode(&howie); err != nil { checkErr(err) } fmt.Printf("FindOne查詢到的數(shù)據(jù):%v\n", howie)
查詢單條數(shù)據(jù)后刪除該數(shù)據(jù)
if err = collection.FindOneAndDelete(getContext(), bson.D{{"name", "howie_3"}}).Decode(&howie); err != nil { checkErr(err) } fmt.Printf("FindOneAndDelete查詢到的數(shù)據(jù):%v\n", howie)
詢單條數(shù)據(jù)后修改該數(shù)據(jù)
if err = collection.FindOneAndUpdate(getContext(), bson.D{{"name", "howie_4"}}, bson.M{"$set": bson.M{"name": "這條數(shù)據(jù)我需要修改了"}}).Decode(&howie); err != nil { checkErr(err) } fmt.Printf("FindOneAndUpdate查詢到的數(shù)據(jù):%v\n", howie)
查詢單條數(shù)據(jù)后替換該數(shù)據(jù)(以前的數(shù)據(jù)全部清空)
if err = collection.FindOneAndReplace(getContext(), bson.D{{"name", "howie_5"}}, bson.M{"hero": "這條數(shù)據(jù)我替換了"}).Decode(&howie); err != nil { checkErr(err) } fmt.Printf("FindOneAndReplace查詢到的數(shù)據(jù):%v\n", howie)
一次查詢多條數(shù)據(jù)(查詢createtime>=3,限制取2條,createtime從大到小排序的數(shù)據(jù))
if cursor, err = collection.Find(getContext(), bson.M{"createtime": bson.M{"$gte": 2}}, options.Find().SetLimit(2), options.Find().SetSort(bson.M{"createtime": -1})); err != nil { checkErr(err) } if err = cursor.Err(); err != nil { checkErr(err) } defer cursor.Close(context.Background()) for cursor.Next(context.Background()) { if err = cursor.Decode(&howie); err != nil { checkErr(err) } howieArrayEmpty = append(howieArrayEmpty, howie) } fmt.Printf("Find查詢到的數(shù)據(jù):%v\n", howieArrayEmpty)
查詢集合里面有多少數(shù)據(jù)
if size, err = collection.Count(getContext(), nil); err != nil { checkErr(err) } fmt.Printf("Count里面有多少條數(shù)據(jù):%d\n", size)
查詢集合里面有多少數(shù)據(jù)(查詢createtime>=3的數(shù)據(jù))
if size, err = collection.Count(getContext(), bson.M{"createtime": bson.M{"$gte": 3}}); err != nil { checkErr(err) } fmt.Printf("Count里面有多少條數(shù)據(jù):%d\n", size)
修改一條數(shù)據(jù)
if updateRes, err = collection.UpdateOne(getContext(), bson.M{"name": "howie_2"}, bson.M{"$set": bson.M{"name": "我要改了他的名字"}}); err != nil { checkErr(err) } fmt.Printf("UpdateOne的數(shù)據(jù):%d\n", updateRes)
修改多條數(shù)據(jù)
if updateRes, err = collection.UpdateMany(getContext(), bson.M{"createtime": bson.M{"$gte": 3}}, bson.M{"$set": bson.M{"name": "我要批量改了他的名字"}}); err != nil { checkErr(err) } fmt.Printf("UpdateMany的數(shù)據(jù):%d\n", updateRes)
刪除一條數(shù)據(jù)
if delRes, err = collection.DeleteOne(getContext(), bson.M{"name": "howie_1"}); err != nil { checkErr(err) } fmt.Printf("DeleteOne刪除了多少條數(shù)據(jù):%d\n", delRes.DeletedCount)
刪除多條數(shù)據(jù)
if delRes, err = collection.DeleteMany(getContext(), bson.M{"createtime": bson.M{"$gte": 7}}); err != nil { checkErr(err) } fmt.Printf("DeleteMany刪除了多少條數(shù)據(jù):%d\n", delRes.DeletedCount)
看完上述內(nèi)容,你們掌握如何在mongodb中使用golang驅(qū)動(dòng)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,感謝各位的閱讀!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。