Go语言实战-golang *** 作Mongodb

Go语言实战-golang *** 作Mongodb,第1张

上一篇文章中提到,使用MySQL存储nginx的处理数据,已经使MySQL处于高并发状态,那么针对这种高并发、海量的数据有没有更合适的存储工具呢?

工欲善其事必先利其器,对各种工具我们都要有所了解,才能在技术选型时,做出更优的方案。

这里对MongoDB做个抛砖引玉的介绍,增删改查

package main

import (
	"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")

	// 连接到MongoDB
	client, 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) {

	// 将李白的年龄增加1
	filter := 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 Student
	err := 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 Student
		err := 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

代码已放码云

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/990302.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存