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/

相关推荐
爱写代码的刚子2 分钟前
C++知识总结
java·开发语言·c++
martian6652 分钟前
QT开发:基于Qt实现的交通信号灯模拟器:实现一个带有倒计时功能的图形界面应用
开发语言·qt
冷琴199610 分钟前
基于java+springboot的酒店预定网站、酒店客房管理系统
java·开发语言·spring boot
缘友一世18 分钟前
macOS .bash_profile配置文件优化记录
开发语言·macos·bash
tekin21 分钟前
macos 中使用macport安装,配置,切换多版本php,使用port 安装php扩展方法总结
开发语言·macos·php·port·mac多版本php安装管理·port-select
CSXB9930 分钟前
一、Python(介绍、环境搭建)
开发语言·python·测试工具·集成测试
火红的小辣椒39 分钟前
PHP反序列化7(字符串逃逸)
开发语言·web安全·php
三玖诶1 小时前
第一弹:C++ 的基本知识概述
开发语言·c++
wjs20241 小时前
Chrome 浏览器:现代网络浏览的先锋
开发语言
爱学的小涛1 小时前
【NIO基础】基于 NIO 中的组件实现对文件的操作(文件编程),FileChannel 详解
java·开发语言·笔记·后端·nio