日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Go语言实战-golang操作Mongodb

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Go语言实战-golang操作Mongodb 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上一篇文章中提到,使用MySQL存儲nginx的處理數據,已經使MySQL處于高并發狀態,那么針對這種高并發、海量的數據有沒有更合適的存儲工具呢?

工欲善其事必先利其器,對各種工具我們都要有所了解,才能在技術選型時,做出更優的方案。

這里對MongoDB做個拋磚引玉的介紹,增刪改查

package mainimport ("context""fmt""log""go.mongodb.org/mongo-driver/bson""go.mongodb.org/mongo-driver/mongo""go.mongodb.org/mongo-driver/mongo/options" )type Student struct {Name string `bson:"name"`Age int `bson:"age"` }func main() {// 設置客戶端連接配置, mongodb://用戶名:密碼@host:port/數據庫clientOptions := options.Client().ApplyURI("mongodb://root:root123@localhost:27017/admin")// 連接到MongoDBclient, err := mongo.Connect(context.TODO(), clientOptions)if err != nil {log.Fatal(err)}// 檢查連接err = client.Ping(context.TODO(), nil)if err != nil {log.Fatal(err)}fmt.Println("Connected to MongoDB!")// 指定獲取要操作的數據集collection := client.Database("test").Collection("c1")// 插入文檔insert(collection)// 修改文檔update(collection)// 查找文檔read(collection)// 刪除文檔delete(collection)// 斷開連接err = client.Disconnect(context.TODO())if err != nil {log.Fatal(err)}fmt.Println("Connection to MongoDB closed.") }func insert(collection *mongo.Collection) {s1 := Student{"歐陽修", 12}s2 := Student{"辛棄疾", 10}s3 := Student{"白居易", 11}// 插入一條數據insertResult, err := collection.InsertOne(context.TODO(), s1)if err != nil {log.Fatal(err)}fmt.Println("Insert one docments success: ", insertResult.InsertedID)// 插入多條數據students := []interface{}{s2, s3}insertManyResult, err := collection.InsertMany(context.TODO(), students)if err != nil {log.Fatal(err)}fmt.Println("Inserted multiple documents success: ", insertManyResult.InsertedIDs) }func update(collection *mongo.Collection) {// 將李白的年齡增加1filter := bson.M{"name": "李白"}update := bson.M{"$inc": bson.M{"age": 1,},}updateResult, err := collection.UpdateOne(context.TODO(), filter, update)if err != nil {log.Fatal(err)}fmt.Printf("Matched %v documents and updated %v documents.\n", updateResult.MatchedCount, updateResult.ModifiedCount) }func read(collection *mongo.Collection) {// 查找李白的文檔filter := bson.M{"name": "李白"}// 創建一個Student變量用來接收查詢的結果var result Studenterr := collection.FindOne(context.TODO(), filter).Decode(&result)if err != nil {log.Fatal(err)}fmt.Printf("Found a single document: %+v\n", result)// 查詢多個// 將選項傳遞給Find()findOptions := options.Find()findOptions.SetLimit(2)// 定義一個切片用來存儲查詢結果var results []*Student// 把bson.D{{}}作為一個filter來匹配所有文檔cur, err := collection.Find(context.TODO(), bson.D{{}}, findOptions)if err != nil {log.Fatal(err)}// 查找多個文檔返回一個光標// 遍歷游標允許我們一次解碼一個文檔for cur.Next(context.TODO()) {// 創建一個值,將單個文檔解碼為該值var elem Studenterr := cur.Decode(&elem)if err != nil {log.Fatal(err)}results = append(results, &elem)}if err := cur.Err(); err != nil {log.Fatal(err)}// 完成后關閉游標cur.Close(context.TODO())fmt.Printf("Found multiple documents (array of pointers): %#v\n", results) }func delete(collection *mongo.Collection) {// 刪除名字是杜甫的文檔deleteResult1, err := collection.DeleteOne(context.TODO(), bson.M{"name": "杜甫"})if err != nil {log.Fatal(err)}fmt.Printf("Deleted %v documents in the trainers collection\n", deleteResult1.DeletedCount)// 刪除所有// deleteResult2, err := collection.DeleteMany(context.TODO(), bson.D{{}})// if err != nil {// log.Fatal(err)// }// fmt.Printf("Deleted %v documents in the trainers collection\n", deleteResult2.DeletedCount) }

MongoDB的增刪改查看的不過癮,請移步

MongoDB 教程 | 菜鳥教程MongoDB 教程 MongoDB 是一個基于分布式文件存儲的數據庫。由 C++ 語言編寫。旨在為 WEB 應用提供可擴展的高性能數據存儲解決方案。 MongoDB 是一個介于關系數據庫和非關系數據庫之間的產品,是非關系數據庫當中功能最豐富,最像關系數據庫的。 現在開始學習 MongoDB! 內容列表 NoSQL 簡介介紹NoSQL基礎概念。 MongoDB 簡介介紹MongoDB基礎概念。 w..https://www.runoob.com/mongodb/mongodb-tutorial.html能看到這也是緣分,再推薦一個很好用的MongoDB開源的可視化工具RoTo 3T

?考慮到官網下載極慢,這里放在百度網盤了

鏈接:https://pan.baidu.com/s/13ZHuSioEDjv8b_NeAgdtXQ?
提取碼:roto

代碼已放碼云

總結

以上是生活随笔為你收集整理的Go语言实战-golang操作Mongodb的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。