mongodb官方的golang驅動基礎使用教程分享
前言
mongo數(shù)據(jù)庫在nodejs平臺有2個常用驅動,mongodb和mongoose,mongodb接口非常接近mongo數(shù)據(jù)庫原生的操作方式,是helloworld之類演示代碼的首選mongo數(shù)據(jù)庫連接驅動,因此成為大部分nodejs初學者最先接觸的mongo數(shù)據(jù)庫驅動。初學者在學會mongo連接的同時,卻也可悲的被helloword這種演示性質(zhì)的數(shù)據(jù)庫操作習慣潛移默化了。
本文主要介紹的是關于mongodb官方的golang驅動使用的相關內(nèi)容,下面話不多說了,來一起看看詳細的介紹吧
使用教程如下:
導入
go get github.com/mongodb/mongo-go-driver/mongo
鏈接mongo服務
if client, err = mongo.Connect(getContext(), url); err != nil { checkErr(err) }
判斷服務是否可用
if err = client.Ping(getContext(), readpref.Primary()); err != nil { checkErr(err) }
選擇數(shù)據(jù)庫和集合
collection = client.Database("testing_base").Collection("howie")
刪除這個集合
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)
完整演示代碼 點擊這里
查看mongo BSON詳細用法 點擊這里
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- golang操作mongodb的方法
- Golang對MongoDB數(shù)據(jù)庫的操作簡單封裝教程
- golang中使用mongo的方法介紹
- golang 連接mongoDB的方法示例
- Golang Mongodb模糊查詢的使用示例
- 利用golang驅動操作MongoDB數(shù)據(jù)庫的步驟
- 詳解Golang使用MongoDB通用操作
- golang連接MongoDB數(shù)據(jù)庫及數(shù)據(jù)庫操作指南
- Golang對mongodb進行聚合查詢詳解
- Go語言學習筆記之golang操作MongoDB數(shù)據(jù)庫
- Golang實現(xiàn)Mongo數(shù)據(jù)庫增刪改查操作
相關文章
Spring Boot中使用MongoDB數(shù)據(jù)庫的方法
MongoDB是一個高性能,開源,無模式的,基于分布式文件存儲的文檔型數(shù)據(jù)庫,由C++語言編寫,其名稱來源取自“humongous”,是一種開源的文檔數(shù)據(jù)庫──NoSql數(shù)據(jù)庫的一種。這篇文章主要介紹了Spring Boot中使用MongoDB數(shù)據(jù)庫的方法,需要的朋友可以參考下2017-12-12mongodb監(jiān)控工具mongostat的使用及命令詳解
mongostat是mongodb自帶的狀態(tài)檢測工具,在命令行下使用,會間隔固定時間獲取mongodb的當前運行狀態(tài),并輸出,本文講述了mongodb監(jiān)控工具mongostat的使用及命令詳解2018-03-03MongoDB自動刪除過期數(shù)據(jù)的方法(TTL索引)
這篇文章主要給大家介紹了關于MongoDB自動刪除過期數(shù)據(jù)(TTL索引)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-11-11