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:[email protected]: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: "[email protected]"})
	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/

相关推荐
程序猿小D1 分钟前
第22节 Node.js JXcore 打包
开发语言·人工智能·vscode·node.js·c#
小猫咪怎么会有坏心思呢8 分钟前
华为OD机试-最短木板长度-二分法(A卷,100分)
java·开发语言·华为od
wo32586614514 分钟前
浪潮交换机配置track检测实现高速公路收费网络主备切换NQA
开发语言·网络·php
知识中的海王19 分钟前
Python html 库用法详解
开发语言·python
獨枭36 分钟前
配置 macOS 上的 Ruby 开发环境
开发语言·macos·ruby
飞由于度41 分钟前
C#中清空DataGridView的方法
开发语言·c#
朝朝又沐沐1 小时前
基于算法竞赛的c++编程(18)string类细节问题
开发语言·c++·算法
黄雪超2 小时前
JVM——对象模型:JVM对象的内部机制和存在方式是怎样的?
java·开发语言·jvm
爱学习的capoo2 小时前
matlab自控仿真【第一弹】❀传递函数和输出时域表达式
开发语言·matlab
EverBule2 小时前
Python 训练 day46
开发语言·python