mongodb官方的golang驅(qū)動基礎(chǔ)使用教程分享
前言
mongo數(shù)據(jù)庫在nodejs平臺有2個常用驅(qū)動,mongodb和mongoose,mongodb接口非常接近mongo數(shù)據(jù)庫原生的操作方式,是helloworld之類演示代碼的首選mongo數(shù)據(jù)庫連接驅(qū)動,因此成為大部分nodejs初學(xué)者最先接觸的mongo數(shù)據(jù)庫驅(qū)動。初學(xué)者在學(xué)會mongo連接的同時,卻也可悲的被helloword這種演示性質(zhì)的數(shù)據(jù)庫操作習(xí)慣潛移默化了。
本文主要介紹的是關(guān)于mongodb官方的golang驅(qū)動使用的相關(guān)內(nèi)容,下面話不多說了,來一起看看詳細的介紹吧
使用教程如下:
導(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")
刪除這個集合
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詳細用法 點擊這里
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- golang操作mongodb的方法
- Golang對MongoDB數(shù)據(jù)庫的操作簡單封裝教程
- golang中使用mongo的方法介紹
- golang 連接mongoDB的方法示例
- Golang Mongodb模糊查詢的使用示例
- 利用golang驅(qū)動操作MongoDB數(shù)據(jù)庫的步驟
- 詳解Golang使用MongoDB通用操作
- golang連接MongoDB數(shù)據(jù)庫及數(shù)據(jù)庫操作指南
- Golang對mongodb進行聚合查詢詳解
- Go語言學(xué)習(xí)筆記之golang操作MongoDB數(shù)據(jù)庫
- Golang實現(xiàn)Mongo數(shù)據(jù)庫增刪改查操作
相關(guān)文章
mongodb 數(shù)據(jù)庫操作詳解--創(chuàng)建,切換,刪除
mongodb是nosql里面最像關(guān)系型數(shù)據(jù)庫的數(shù)據(jù)庫。單表操作,基本上可以和關(guān)系型數(shù)據(jù)庫差不多。mongodb比較易學(xué),易用,分幾期記錄一下,學(xué)習(xí)和使用mongodb過程。2014-07-07
MongoDB運行狀態(tài)監(jiān)控、性能分析工具mongostat詳解
這篇文章主要介紹了MongoDB運行狀態(tài)監(jiān)控、性能分析工具mongostat詳解,mongostat是mongdb自帶的狀態(tài)檢測工具,在命令行下使用,它會間隔固定時間獲取mongodb的當(dāng)前運行狀態(tài),并輸出,本文詳細講解了它的使用,需要的朋友可以參考下2015-07-07
老生常談MongoDB數(shù)據(jù)庫基礎(chǔ)操作
下面小編就為大家?guī)硪黄仙U凪ongoDB數(shù)據(jù)庫基礎(chǔ)操作。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07

