- 环境安装
https://blog.csdn.net/qq_42402648/article/details/134932412
net start MongoDB 启动
net stop MongoDB 停止 - 测试插入数据结构
java
{
"create_time": {
"$data": "1711244538176"
},
"language": "en",
"create_time01": "1711244538176",
"source": "ximalaya",
"time": 848
}
- 数据基础查询
java
查询source为ximalaya
{"source":"ximalaya"}
查询source为ximalaya,language为 zh
{"source":"ximalaya","language":"zh"}
查询source为ximalaya,time<300
{"source":"ximalaya","time":{"$lt":300}}
查询source为ximalaya,language为zh或者ko
{"source":"ximalaya","language":{"$in":["jp","ko"]}}
查询source为null false为null,true 不为null
{"source":{"$exists":false}}
根据time降序 -1降序,1 升序
{"time":-1}
- 聚合查询
java
统计language为zh的总数
[
{
$match: {
language: "zh",
}
},
{
$count: 'language_count'
}
]
根据source分组统计总数
[
{
$group: {
_id: "$source",
count: {
$sum: 1
}
}
}
]
根据source分组统计所有time和
[
{
$group: {
_id: "$source",
count: {
$sum: 1,
},
time_sum: {
$sum: "$time",
},
},
},
]
数组文档拆分 ,一拆多行数据
[
{
$unwind: "$tag"
},
]
关联表查询
[
{
$match: {
source: "qintin",
},
},
{
$lookup: {
from: "base_info_v2",
localField: "v2_id",
foreignField: "id",
as: "v2Info",
},
},
]
- 管道操作符
java
$project 指定输出的字段
[
{
$project: {
sourceName: "$source",
date:"$time",
},
},
]
$match 添加过滤条件
[
{
$match: {
source:"youtube"
}
},
{
$project: {
sourceName: "$source",
date:"$time",
},
},
]
$count 统计结果总数
[
{
$match: {
source:"youtube"
}
},
{
$count: 'sourceCount'
}
]
分组后将输出结果使用project重新命名
[
{
$group: {
_id: "$source",
count:{
$sum:1
}
}
},
{
$project: {
source:"$_id",
count:"$count"
}
}
]
根据时间分组(时间格式化)
再根据source分组 统计出每天每个来源的总数
将分组后的数据重新命名 ,再次根据时间分组
最后 根据时间 升序排序
java
[
{
$group: {
_id: {
date: {
$dateToString: {
format: "%Y-%m-%d",
date: "$create_time",
},
},
source: "$source",
},
count: {
$sum: 1,
},
},
},
{
$project: {
date: "$_id.date",
source: "$_id.source",
count: "$count",
},
},
{
$group: {
_id: "$date",
positions: {
$push: {
source: "$source",
count: "$count",
},
},
},
},
{
$sort: {
_id: 1
}
}
]