Golang實(shí)現(xiàn)Mongo數(shù)據(jù)庫(kù)增刪改查操作
本文將通過一個(gè)簡(jiǎn)單的 Go 語(yǔ)言示例,介紹如何使用 MongoDB 進(jìn)行基本的數(shù)據(jù)操作,包括插入、查詢、更新和刪除操作。我們將使用 MongoDB 的官方 Go 驅(qū)動(dòng)程序來與數(shù)據(jù)庫(kù)進(jìn)行交互。
一、引言
MongoDB 是一款流行的 NoSQL 數(shù)據(jù)庫(kù),它使用文檔存儲(chǔ)結(jié)構(gòu),可以存儲(chǔ)非常復(fù)雜的數(shù)據(jù)類型。Go 語(yǔ)言以其簡(jiǎn)潔和高效的特性,成為越來越多開發(fā)者選擇的編程語(yǔ)言。在本文中,我們將結(jié)合 Go 語(yǔ)言和 MongoDB,展示如何實(shí)現(xiàn)基本的數(shù)據(jù)操作。
二、環(huán)境準(zhǔn)備
安裝 MongoDB:請(qǐng)參考 MongoDB 的官方文檔進(jìn)行安裝。
安裝 Go 語(yǔ)言環(huán)境:請(qǐng)參考 Go 語(yǔ)言的官方文檔進(jìn)行安裝。
安裝 MongoDB Go 驅(qū)動(dòng)程序:在終端中運(yùn)行
go get go.mongodb.org/mongo-driver/mongo。
三、代碼實(shí)現(xiàn)
以下是一個(gè)簡(jiǎn)單的 Go 語(yǔ)言示例,展示了如何使用 MongoDB 進(jìn)行數(shù)據(jù)操作。
1. 連接到 MongoDB
func MongoClient() *mongo.Client {
clientOptions := options.Client().ApplyURI("mongodb://10.90.45.1:27017/?connect=direct")
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
return client
}
2. 插入數(shù)據(jù)
func InsertOne(client *mongo.Client, databaseName string, collectionName string, doc bson.M) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
collection := client.Database(databaseName).Collection(collectionName)
_, err := collection.InsertOne(ctx, doc)
return err
}
func InsertMany(client *mongo.Client, databaseName string, collectionName string, doc []interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
collection := client.Database(databaseName).Collection(collectionName)
_, err := collection.InsertMany(ctx, doc)
return err
}
3. 查詢數(shù)據(jù)
func Find(client *mongo.Client, databaseName string, collectionName string, filter bson.M) ([]bson.M, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
collection := client.Database(databaseName).Collection(collectionName)
cur, err := collection.Find(ctx, filter)
if err != nil {
return nil, err
}
defer cur.Close(ctx)
var results []bson.M
for cur.Next(ctx) {
var result bson.M
err := cur.Decode(&result)
if err != nil {
return nil, err
}
results = append(results, result)
}
if err := cur.Err(); err != nil {
return nil, err
}
return results, nil
}
4. 更新數(shù)據(jù)
func UpdateOne(client *mongo.Client, databaseName string, collectionName string, filter bson.M, update bson.M) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
collection := client.Database(databaseName).Collection(collectionName)
_, err := collection.UpdateOne(ctx, filter, bson.M{"$set": update})
return err
}
func UpdateMany(client *mongo.Client, databaseName string, collectionName string, filter bson.M, update bson.M) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
collection := client.Database(databaseName).Collection(collectionName)
_, err := collection.UpdateMany(ctx, filter, bson.M{"$set": update})
return err
}
5. 刪除數(shù)據(jù)
func DeleteOne(client *mongo.Client, databaseName string, collectionName string, filter bson.M) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
collection := client.Database(databaseName).Collection(collectionName)
_, err := collection.DeleteOne(ctx, filter)
return err
}
func DeleteMany(client *mongo.Client, databaseName string, collectionName string, filter bson.M) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
collection := client.Database(databaseName).Collection(collectionName)
_, err := collection.DeleteMany(ctx, filter)
return err
}
6.Main執(zhí)行
package main
import (
"context"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
client := MongoClient()
defer client.Disconnect(context.TODO())
dbName := "mydatabase"
dcName := "mycollection"
// 插入操作
record := bson.M{"name": "John Doe", "age": 30}
if err := InsertOne(client, dbName, dcName, record); err != nil {
log.Fatal(err)
}
records := []interface{}{
bson.M{"name": "Taylor Smith", "sex": "male", "age": 27},
bson.M{"name": "Lisa Rune", "sex": "female", "age": 28},
bson.M{"name": "Lily", "sex": "female", "age": 28},
bson.M{"name": "Alex", "sex": "female", "age": 26},
bson.M{"name": "Alisa", "sex": "female", "age": 19},
bson.M{"name": "Tom", "sex": "male", "age": 28},
bson.M{"name": "Felix", "sex": "male", "age": 32},
bson.M{"name": "Richard", "sex": "male", "age": 30},
}
if err := InsertMany(client, dbName, dcName, records); err != nil {
log.Fatal(err)
}
// 查詢操作
results, err := Find(client, dbName, dcName, bson.M{"age": 30})
if err != nil {
log.Fatal(err)
}
for _, result := range results {
fmt.Println(result)
}
// 更新操作
if err := UpsertOne(client, dbName, dcName, bson.M{"name": "Lisa Rune"}, bson.M{"age": 31}); err != nil {
log.Fatal(err)
}
if err := UpdateMany(client, dbName, dcName, bson.M{"sex": "male"}, bson.M{"age": 31}); err != nil {
log.Fatal(err)
}
// 刪除操作
if err := DeleteOne(client, dbName, dcName, bson.M{"name": "John Doe"}); err != nil {
log.Fatal(err)
}
if err := DeleteMany(client, dbName, dcName, bson.M{"sex": "female"}); err != nil {
log.Fatal(err)
}
}
7.結(jié)果

四、總結(jié)
本文通過一個(gè)簡(jiǎn)單的 Go 語(yǔ)言示例,介紹了如何使用 MongoDB 進(jìn)行基本的數(shù)據(jù)操作。我們使用了 MongoDB 的官方 Go 驅(qū)動(dòng)程序,實(shí)現(xiàn)了插入、查詢、更新和刪除操作。希望這個(gè)示例能夠幫助您更好地了解如何在 Go 語(yǔ)言中使用 MongoDB 進(jìn)行數(shù)據(jù)操作。
到此這篇關(guān)于Golang實(shí)現(xiàn)Mongo數(shù)據(jù)庫(kù)增刪改查操作的文章就介紹到這了,更多相關(guān)Golang Mongo增刪改查內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- golang操作mongodb的方法
- Golang對(duì)MongoDB數(shù)據(jù)庫(kù)的操作簡(jiǎn)單封裝教程
- golang中使用mongo的方法介紹
- golang 連接mongoDB的方法示例
- mongodb官方的golang驅(qū)動(dòng)基礎(chǔ)使用教程分享
- Golang Mongodb模糊查詢的使用示例
- 利用golang驅(qū)動(dòng)操作MongoDB數(shù)據(jù)庫(kù)的步驟
- 詳解Golang使用MongoDB通用操作
- golang連接MongoDB數(shù)據(jù)庫(kù)及數(shù)據(jù)庫(kù)操作指南
- Golang對(duì)mongodb進(jìn)行聚合查詢?cè)斀?/a>
- Go語(yǔ)言學(xué)習(xí)筆記之golang操作MongoDB數(shù)據(jù)庫(kù)
相關(guān)文章
Go語(yǔ)言在終端打開實(shí)現(xiàn)進(jìn)度條處理數(shù)據(jù)方法實(shí)例
這篇文章主要介紹了Go語(yǔ)言在終端打開實(shí)現(xiàn)進(jìn)度條處理數(shù)據(jù)方法實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Golang 操作 Kafka 如何設(shè)置消息的失效時(shí)間
在使用 Golang 操作 Kafka 時(shí),你可以使用 Sarama 庫(kù)來設(shè)置消息的失效時(shí)間,這篇文章主要介紹了Golang操作Kafka設(shè)置消息的失效時(shí)間,需要的朋友可以參考下2023-06-06
go語(yǔ)言定義零值可用的類型學(xué)習(xí)教程
這篇文章主要為大家介紹了go語(yǔ)言定義零值可用的類型教程學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Golang中如何對(duì)MySQL進(jìn)行操作詳解
這篇文章主要給大家介紹了關(guān)于在Golang中如何對(duì)MySQL進(jìn)行操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Golang具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

