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/

相关推荐
AIMath~3 分钟前
python中的uv命令揭秘
开发语言·python·uv
弹简特6 分钟前
【零基础学Python】06-Python模块和包、异常处理、文件常用操作
开发语言·python
x***r1518 分钟前
Postman-win64-7.2.2-Setup安装步骤详解(附API接口测试与参数配置教程)
开发语言·lua
念恒1230616 分钟前
Python 面向对象编程核心:对象、实例化、封装与变量作用域
开发语言·python
大菜菜小个子20 分钟前
template<typename T>使用
java·开发语言·算法
L_090724 分钟前
【C++】C++11 新特性
开发语言·c++
方也_arkling27 分钟前
【Java-Day15】API篇-ArrayList集合
java·开发语言
我是一颗柠檬30 分钟前
【Java后端技术亮点】动态路由权限(按钮级权限),细粒度控制到按钮级别
java·开发语言·后端·状态模式
Fanfanaas32 分钟前
C++ 继承
java·开发语言·jvm·c++·学习·算法
在繁华处44 分钟前
Java从零到熟练(十一):Spring框架入门
java·开发语言·spring