当前位置: 首页 > news >正文

go MongoDB

  1. 安装
go get go.mongodb.org/mongo-driver/mongo 
package mongodbexampleimport ("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.Clientvar testCollection *mongo.Collectionfunc 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.InsertedIDfmt.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.MongoStructcur.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/

http://www.lryc.cn/news/139348.html

相关文章:

  • 算法与数据结构(八)--优先队列
  • React 全栈体系(三)
  • 腾讯云下一代CDN -- EdgeOne加速MinIO对象存储
  • GitLab-CI 指南
  • MyBatis的核心技术掌握,简单易懂(上)
  • Redisson自定义序列化
  • MongoDB Long 类型 shell 查询
  • 回归预测 | MATLAB实现GA-APSO-IBP改进遗传-粒子群算法优化双层BP神经网络多输入单输出回归预测
  • Spring cache整合Redis使用介绍
  • Metasploit提权
  • TypeScript三种特殊类型
  • 如何使用CSS实现一个响应式轮播图?
  • 数据生成 | MATLAB实现MCMC马尔科夫蒙特卡洛模拟的数据生成
  • 【从零开始的rust web开发之路 二】axum中间件和共享状态使用
  • Vue操作时间
  • 数据库——Redis 常见数据结构以及使用场景分析
  • 数学建模-规划工具箱yalmip
  • [SQL挖掘机] - 窗口函数 - 计算移动平均
  • 域名和hostname
  • echarts 甘特图一组显示多组数据
  • 1139. 最大的以 1 为边界的正方形;2087. 网格图中机器人回家的最小代价;1145. 二叉树着色游戏
  • css滚动条的使用
  • 优化Python代理爬虫的应用
  • [C++] STL_vector使用与常用接口的模拟实现
  • 【LeetCode】167. 两数之和 II - 输入有序数组 - 双指针
  • YOLOV1
  • 美团增量数仓建设新进展
  • ​LeetCode解法汇总2337. 移动片段得到字符串
  • Fpass与Fstop
  • Java快速入门体验