go MongoDB

  1. 安装
bash 复制代码
go get go.mongodb.org/mongo-driver/mongo 
go 复制代码
package mongodbexample

import (
	"context"
	"fmt"
	"ginapi/structs"
	"time"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

var mongoClient *mongo.Client

var testCollection *mongo.Collection

func InitMongo() {
	clientOptions := options.Client().ApplyURI("mongodb://ellis:ellischen@192.168.214.133:32000/")
	mongoClient, _ := mongo.Connect(context.TODO(), clientOptions)
	testCollection = mongoClient.Database("baz").Collection("qux")
}

func InsertOneByStruct() {
	res, err := testCollection.InsertOne(context.Background(), &structs.MongoStruct{Id: primitive.NewObjectID(), UserName: "ellis", Email: "849773373@qq.com"})
	if err != nil {
		fmt.Printf("err: %v\n", err)
	}
	id := res.InsertedID
	fmt.Printf("id: %v\n", id)
}

func InsertManyByStructs() {
	values := []interface{}{structs.MongoStruct{Id: primitive.NewObjectID(), UserName: "1", Email: "1"}, structs.MongoStruct{Id: primitive.NewObjectID(), UserName: "2", Email: "2"}}
	imr, _ := testCollection.InsertMany(context.Background(), values)
	fmt.Printf("imr.InsertedIDs: %v\n", imr.InsertedIDs)
}

func FindALL() {
	ctx, channel := context.WithTimeout(context.Background(), 30*time.Second)
	defer channel()

	// cur, _ := testCollection.Find(ctx, bson.M{"username": "1"})
	cur, _ := testCollection.Find(ctx, bson.D{{"username", "1"}})
	defer cur.Close(ctx)
	for cur.Next(ctx) {
		var value structs.MongoStruct
		cur.Decode(&value)
		fmt.Printf("value: %v\n", value)
	}
}

func UpdateMany() {
	ctx, channel := context.WithTimeout(context.Background(), 30*time.Second)
	defer channel()
	ur, err := testCollection.UpdateMany(ctx, bson.D{{"username", "vv"}}, bson.D{{"$set", bson.D{{"username", "ellis"}, {"email", "haha"}}}})
	if err != nil {
		fmt.Printf("err: %v\n", err)
	}
	fmt.Printf("ur.MatchedCount: %v\n", ur.MatchedCount)
}

func DeleteOne() {
	ctx, channel := context.WithTimeout(context.Background(), 30*time.Second)
	defer channel()
	dr, err := testCollection.DeleteOne(ctx, bson.D{{"username", "1"}})
	if err != nil {
		fmt.Printf("err: %v\n", err)
	}
	fmt.Printf("dr.DeletedCount: %v\n", dr.DeletedCount)
}

// func main() {
// 	InitMongo()
// 	// InsertOneByStruct()
// 	// InsertManyByStructs()
// 	// FindALL()
// 	// UpdateMany()
// 	DeleteOne()
// }

https://ocakhasan.github.io/golang-mongodb-query-examples/

相关推荐
HL_LOVE_C几秒前
全面理解-回调函数CallBack
开发语言·c++
Distance失落心5 分钟前
java基于数组实现队列(四)
java·开发语言·数据结构·算法·面试·java-ee·intellij-idea
blog_wanghao15 分钟前
C#: 日志函数
开发语言·c#·日志
weixin_3077791319 分钟前
PySpark实现GROUP BY WITH CUBE和WITH ROLLUP的分类汇总功能
大数据·开发语言·python·spark
信徒_28 分钟前
go 语言中的线程池
开发语言·后端·golang
Pandaconda28 分钟前
【Golang 面试题】每日 3 题(六十五)
开发语言·经验分享·笔记·后端·面试·golang·go
至暗时刻darkest29 分钟前
go 查看版本
开发语言·后端·golang
向哆哆43 分钟前
Java与NoSQL数据库的集成与优化
java·开发语言·nosql
MSTcheng.1 小时前
【C语言】指针(5)
c语言·开发语言
╮壞孩子的天1 小时前
C语言多人聊天室 ---chat(客户端聊天)
c语言·开发语言