go对mongodb的聚合查询

mongodb的环境搭建参考前面一篇通过mongo-driver使用说明 GO 包管理机制
BSON 介绍
在Go中使用BSON对象构建操作命令
在我们发送查询给数据库之前, 很重要的一点是,理解Go Driver是如何和BSON对象协同工作的。
JSON文档在MongoDB里面以二进制形式存储, 被称作BSON(二进制编码的JSON)。不像其他的数据库保存JSON数据为简单的字符串和数字, BSON扩展了JSON的保存形式, 包括额外的类型, 比如int, long, date, floating point以及decimal128。这使得它让应用程序更容易来可靠地处理、排序和比较数据。
Go Driver有两个系列的类型表示BSON数据:D系列类型和Raw系列类型。
D系列的类型使用原生的Go类型简单地构建BSON对象。这可以非常有用的来创建传递给MongoDB的命令。 D系列包含4种类型:
– D:一个BSON文档。这个类型应该被用在顺序很重要的场景, 比如MongoDB命令。
– M: 一个无需map。 它和D是一样的, 除了它不保留顺序。
– A: 一个BSON数组。
– E: 在D里面的一个单一的子项。
这里有一个使用D类型构建的过滤文档的例子, 它可能被用在查询name字段匹配“Alice”或者“Bob”的文档:
bson.D{{
"name",
bson.D{{
"$in",
bson.A{"Alice", "Bob"}
}}
}}
过滤查询
go 查询
package main
import (
"context"
"fmt"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/bson"
)
type Student struct {
Name string
Score int
}
func main() {
// 设置客户端连接配置
clientOptions := options.Client().ApplyURI("mongodb://admin:123456@localhost:27017")
// 连接到MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
// 检查连接
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB!")
collection := client.Database("demo").Collection("student")
s1 := Student{"小红", 66}
s2 := Student{"小兰", 70}
s3 := Student{"小黄", 86}
s4 := Student{"小张", 92}
students := []interface{}{s1, s2, s3, s4}
res, err := collection.InsertMany(context.TODO(), students)
if err != nil {
log.Fatal(err)
}
fmt.Println("插入数据完成", res.InsertedIDs)
aggreData := []map[string]interface{}{}
scoreArray := [5]int{60, 70, 80, 90, 100}
for i := 0; i<len(scoreArray)-1; i++{
filter := bson.M{
"score": bson.M{
"$gt": scoreArray[i],
"$lt": scoreArray[i+1],
},
}
count, err := collection.CountDocuments(context.TODO(), filter)
if err != nil {
log.Printf("get metric error %s", err)
}
if count > 0 {
temp := map[string]interface{}{}
temp["count"] = count
aggreData = append(aggreData, temp)
}
}
fmt.Println("get finish!")
for _, value := range aggreData {
fmt.Println(value)
}
}
admin> use demo
switched to db demo
demo> show tables
student
demo> db.student.find()
[
{ _id: ObjectId("63e25fa9c6fe361e9f5e7e6e"), name: '小红', score: 66 },
{ _id: ObjectId("63e25fa9c6fe361e9f5e7e6f"), name: '小兰', score: 70 },
{ _id: ObjectId("63e25fa9c6fe361e9f5e7e70"), name: '小黄', score: 86 },
{ _id: ObjectId("63e25fa9c6fe361e9f5e7e71"), name: '小张', score: 92 }
]
➜ mongo go run hello.go
Connected to MongoDB!
插入数据完成 [ObjectID("63e26329b1f99ae321f895c2") ObjectID("63e26329b1f99ae321f895c3") ObjectID("63e26329b1f99ae321f895c4") ObjectID("63e26329b1f99ae321f895c5")]
get finish!
map[count:1]
map[count:1]
map[count:1]
bucket命令
以上的命令其实是用来替换bucket命令。bucket命令是3.4的新功能,功能是可以对传入的一组查询数据分组查询并统计。如传入查询数据为[0, 10, 20, 30],可以查询出0~10的数据,10~20的数据。
cond := make([]bson.M, 0)
cond = append(cond, bson.M{"$match": bson.M{"user_id": userID}})
cond = append(cond, bson.M{
"$bucket": bson.D{
bson.E{Key: "groupBy", Value: "$" + queryField},
bson.E{Key: "default", Value: "_others_"},
bson.E{Key: "boundaries", Value: boundries},
bson.E{Key: "output", Value: bson.D{bson.E{Key: "count", Value: bson.M{"$sum": 1}}}},
}})
aggreData := s.aggregateMongoQuery(collection, cond)
聚合查询
mongo命令使用
使用mongodb肯定离不开聚合查询这种威力强大的操作。下面实现一个提取数组中元素,重命名,匹配区间,统计总数这样一个操作,使用到的命令包括:
- unwind:将数组中的每一个值拆分为单独的文档
- project:提取文档中元素或重命名
- match:匹配文档中元素
- group:统计文档
插入数据
demo> db.student.insert({"field": "score", "num": [12, 23, 34, 45, 56, 67, 78, 89]})
{
acknowledged: true,
insertedIds: { '0': ObjectId("63e3b26b029e307c2c8c46e6") }
}
mongo命令查询
demo> db.student.aggregate([{ $unwind:"$num"}, {$project:{value: "$num"}}, {$match: {value:{$gte: 40, $lt: 90}}}, {$group: {_id: "in_40_90", count: {$sum: 1}}}])
[ { _id: 'in_40_90', count: 5 } ]
程序就是将以上命令翻译成go语言执行
go 聚合查询
package main
import (
"context"
"fmt"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/bson"
)
type Student struct {
Name string
Score int
}
func main() {
// 设置客户端连接配置
clientOptions := options.Client().ApplyURI("mongodb://admin:123456@localhost:27017")
// 连接到MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
// 检查连接
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB!")
collection := client.Database("demo").Collection("student")
unwind := bson.D{{"$unwind", "$num"}}
project := bson.D{{
"$project", bson.D{{
"value", "$num",
}},
}}
match := bson.D{{
"$match", bson.D{{
"value", bson.D{{"$gte", 40}, {"$lt", 90}},
}},
}}
group := bson.D{{
"$group", bson.D{
{"_id", nil},
{"count", bson.D{{"$sum", 1}}},
},
}}
showInfoCursor, err := collection.Aggregate(context.TODO(), mongo.Pipeline{unwind, project, match, group})
var showsWithInfo []map[string]interface{}
if err = showInfoCursor.All(context.TODO(), &showsWithInfo); err != nil {
log.Printf("collection %s", err)
panic(err)
}
for _, value := range showsWithInfo {
fmt.Println(value)
}
}
➜ mongo go run hello.go
Connected to MongoDB!
map[_id:<nil> count:5]
可以看到结果和mongo命令是一样的
go对mongodb的聚合查询的更多相关文章
- 使用morphia实现对mongodb的聚合查询
morphia是谷歌的一个针对mongodb的数据化持久框架: 关于mongodb的介绍不在这里展示,直接进入主题:采用morphia实现对mongodb的聚合查询 这里获取所有学生的分数总和 spr ...
- mongodb 高级聚合查询
mongodb高级聚合查询 在工作中会经常遇到一些mongodb的聚合操作,特此总结下.mongo存储的可以是复杂类型,比如数组.对象等mysql不善于处理的文档型结构,并且聚合的操作也比mysq ...
- mongodb高级聚合查询
在工作中会经常遇到一些mongodb的聚合操作,特此总结下.mongo存储的可以是复杂类型,比如数组.对象等mysql不善于处理的文档型结构,并且聚合的操作也比mysql复杂很多. 注:本文基于 mo ...
- mongodb高级聚合查询(转)
在工作中会经常遇到一些mongodb的聚合操作,特此总结下.mongo存储的可以是复杂类型,比如数组.对象等mysql不善于处理的文档型结构,并且聚合的操作也比mysql复杂很多. 注:本文基于 mo ...
- MongoDB的使用学习之(七)MongoDB的聚合查询(两种方式)附项目源码
先来张在路上…… 铛铛铛……项目源码下载地址:http://files.cnblogs.com/ontheroad_lee/MongoDBDemo.rar 此项目是用Maven创建的,没有使用Mave ...
- 【Mongodb】聚合查询 && 固定集合
概述 数据存储是为了可查询,统计.若数据只需存储,不需要查询,这种数据也没有多大价值 本篇介绍Mongodb 聚合查询(Aggregation) 固定集合(Capped Collections) 准备 ...
- MongoDB——》聚合查询(project、match、limit、skip、unwind、group、sort)
https://blog.csdn.net/weixin_43453386/article/details/85065043#1_testweightname_id_35 https://blog.c ...
- mongodb的高级查询
db的帮助文档 输入:db.help(); db.AddUser(username,password[, readOnly=false]) 添加用户 db.auth(usrename,passwor ...
- python操作mongodb之二聚合查询
#聚合查询 from pymongo import MongoClient db = MongoClient('mongodb://10.0.0.9:27017/').aggregation_exam ...
- mongodb聚合查询-aggregate
Mongodb-aggregate 在工作中经常遇到一些mongodb的聚合操作,和mysql对比起来,mongo存储的可以是复杂的类型,比如数组,字典等mysql不善于处理的文档型结构,但是mong ...
随机推荐
- 【scipy 基础】--图像处理
SciPy库本身是针对科学计算而不是图像处理的,只是图像处理也包含了很多数学计算,所以Scipy也提供了一个专门的模块ndimage用于图像处理. ndimage模块提供的功能包括输入/输出图像.显示 ...
- 架构探索之路-第一站-clickhouse
一.前言 架构, 软件开发中最熟悉不过的名词, 遍布在我们的日常开发工作中, 大到项目整体, 小到功能组件, 想要实现高性能.高扩展.高可用的目标都需要优秀架构理念辅助. 所以本人尝试编写架构系列文章 ...
- windows Server 2008 r2 无法通过update更新的解决方法
注意:目前windows Server系列操作系统已经完全停止支持. 1,安装 SP1补丁 KB976932 点击:微软补丁下载网站 搜索KB976932,手动下载安装. 2,安装 KB4474419 ...
- Netty源码学习7——netty是如何发送数据的
零丶引入 系列文章目录和关于我 经过<Netty源码学习4--服务端是处理新连接的&netty的reactor模式和<Netty源码学习5--服务端是如何读取数据的>,我们了 ...
- KNN算法实战——海伦约会(KDtree优化)
本文通过海伦约会的例子来测试之前写的KDTree的效果,并且探讨了特征是否进行归一化对整个模型的表现的影响.最后发现在机器学习中,特征归一化确实对模型能提供非常大的帮助. 1 from KDTree ...
- Netty源码学习9——从Timer到ScheduledThreadPoolExecutor到HashedWheelTimer
系列文章目录和关于我 一丶前言 之前在学习netty源码的时候,经常看netty hash时间轮(HashedWheelTimer)的出现,时间轮作为一种定时调度机制,在jdk中还存在Timer和Sc ...
- python tkinter 使用(三)
python tkinter 使用(三) 本篇文章主要讲下tkinter下的filedialog的使用. 1: askopenfilename 首先使用tkinter中fiedialog来实现一个简单 ...
- MinIO客户端之cp
MinIO提供了一个命令行程序mc用于协助用户完成日常的维护.管理类工作. 官方资料 mc cp 上传文件至指定桶内,命令如下: ./mc cp ./local.json local1/bkt1/ 控 ...
- ElasticSearch之cat repositories API
命令样例如下: curl -X GET "https://localhost:9200/_cat/repositories?v=true&pretty" --cacert ...
- java:JDBC使用步骤(mysql)java小白的第一篇博客
1:环境要求 先在你的mysql里面创造一个数据库(并添加一点数据) 下载并导入驱动 2:jdbc的五大步骤 第一步:加载mysql驱动: 1 Class.forName("com.mysq ...