在 Redash 中使用 MongoDB 示例
统计文件的总数
将组的 _id 设为 null。
{
"collection": "mail_logs",
"aggregate": [
{
"$group": {
"_id": null,
"cnt": {
"$sum": 1
}
}
}
]
}
在中国母语中重新表述此句:
进行除法运算的示例
{
"collection": "campaign_mails",
"aggregate": [
{
"$match": {
"created_at": { "$gt" : {"$humanTime": "last month"} },
"statistics": { "$ne": null }
}
},
{ "$limit": 800 },
{
"$project": {
"created_at": "$created_at",
"site_id": "$site_id",
"subject": "$subject",
"processed_count": "$statistics.processed_count",
"delivered_count": "$statistics.delivered_count",
"opened_count": "$statistics.opened_count",
"clicked_count": "$statistics.clicked_count",
"CTR": {
"$cond": [
{ "$eq": [ "$statistics.opened_count", 0 ] },
"N/A",
{ "$divide": ["$statistics.clicked_count", "$statistics.opened_count"] }
]
},
"OpenRate": {
"$cond": [
{ "$eq": [ "$statistics.delivered_count", 0 ] },
"N/A",
{ "$divide": ["$statistics.opened_count", "$statistics.delivered_count"] }
]
}
}
},
{
"$sort": [
{
"name": "processed_count",
"direction": -1
}
]
}
]
}
小组进行示范
{
"collection": "campaign_mails",
"aggregate": [
{
"$match": {
"created_at": { "$gt" : {"$humanTime": "last month"} },
"statistics": { "$ne": null }
}
},
{ "$limit": 800 },
{
"$group": {
"_id": "$site_id",
"count": {
"$sum": 1
},
"processed_count": {
"$sum": "$statistics.processed_count"
},
"delivered_count": {
"$sum": "$statistics.delivered_count"
},
"opened_count": {
"$sum": "$statistics.opened_count"
}
}
}
]
}
查找一个示例进行参考
{
"collection": "campaign_mails",
"aggregate": [
{
"$match": {
"created_at": { "$gt": { "$humanTime": "last month" } }
}
},
{
"$group": {
"_id": "$site_id",
"count": {
"$sum": 1
}
}
},
{
"$lookup": {
"from": "sites",
"localField": "_id",
"foreignField": "_id",
"as": "sites"
}
},
{
"$unwind": "$sites"
},
{
"$project": {
"site_title": "$sites.title",
"count": "$count"
}
}
]
}
用月份统计的样本

完成的图像如上所示。
请将以下查询进行概述。
{
"collection": "sample_collection",
"aggregate": [
{
"$group": {
"_id": {
"year": { "$year": "$created_at" },
"month": { "$month": "$created_at" }
},
"count": { "$sum": 1 }
}
},
{
"$addFields": {"yearmonth": {"$add": [{"$multiply": ["$_id.year",100]}, "$_id.month"]}}
},
{
"$project": {
"count": 1,
"yearmonth": 1
}
}
]
}
由于无法使用$toString或$convert来连接年份和月份,所以我只能强行使用数字处理。
我一开始这样想的(附加说明),不过使用 $dateToString 可以很好地处理。
{
"collection": "customer_lists",
"aggregate": [
{
"$group": {
"_id": { "$dateToString": { "format": "%Y-%m", "date": "$created_at" } },
"count": { "$sum": 1 }
}
},
{
"$sort": [
{ "name": "_id", "direction": 1 }
]
}
]
}
在ObjectId字段上进行匹配条件的检查
{
"collection": "customers",
"aggregate": [
{
"$match": {
"site_id": { "$nin": [
{ "$oid": "552242d169702d1b40c61300" },
{ "$oid": "543d1e3e69702d0c76240000" }
]}
}
},
{
"$group": {
"_id": {"$year": "$created_at" },
"count": { "$sum": 1 }
}
}
]
}
允许输入日期

{
"collection": "contacts",
"aggregate": [
{
"$match": {
"created_at": { "$gt" : {"$humanTime": "{{ from }}"} }
}
},
{
"$group": {
"_id": {
"year": { "$year": "$created_at" },
"month": { "$month": "$created_at" },
"day": { "$dayOfMonth": "$created_at" }
},
"count": { "$sum": 1 }
}
},
{
"$addFields": {
"ymd": { "$add": [
{ "$multiply": ["$_id.year", 10000] },
{ "$multiply": ["$_id.month", 100] },
"$_id.day"
]}
}
},
{
"$project": {
"_id": 0,
"ymd": 1,
"count": 1
}
}
]
}
日期格式
"$project": {
"created_at": {
"$dateToString": {
"format": "%Y-%m-%d %H:%M:%S",
"date": "$created_at"
}
}
}
地图
{
"collection": "mail_action_histories",
"aggregate": [
{
"$project": { "mail_actions": {
"$map": {
"input": "$mail_actions",
"as": "ma",
"in": {
"ation_type": "$$ma.action_type",
"msg_id": "$$ma.mail_msg_id"
}
}
} }
},
{
"$limit": 2
}
]
}
我只想要计数
{
"collection": "totalize_cvs",
"query": {
"type": "conversion"
},
"count": true
}
{
"collection": "customer_lists",
"aggregate": [
{
"$group": {
"_id": null,
"count": { "$sum": 1 }
}
}
]
}
根据条件将计数逻辑分开
举个例子,当type_create根据导入/手动分类时,想要统计每一个类别的数量。
{
"collection": "customer_lists",
"aggregate": [
{
"$group": {
"_id": {
"$dateToString": { "format": "%Y-%m", "date": "$created_at" }
},
"cnt_import": { "$sum": { "$cond": [{"$eq": ["$type_create", "import"]}, 1, 0] } },
"cnt_manual": { "$sum": { "$cond": [{"$eq": ["$type_create", "manual"]}, 1, 0] } }
}
},
{
"$sort": [
{ "name": "_id", "direction": 1 }
]
}
]
}
除了使用$cond的方法外,还有使用$switch的方式来实现。
"cnt_import": { "$sum": { "$cond": [{"$eq": ["$type_create", "import"]}, 1, 0] } },
"cnt_manual": { "$sum": { "$cond": [{"$eq": ["$type_create", "manual"]}, 1, 0] } },
"cnt_other": { "$sum": { "$switch": {
"branches": [
{ "case": { "$eq": [ "$type_create", "import" ] }, "then": 0 },
{ "case": { "$eq": [ "$type_create", "manual" ] }, "then": 0 }
],
"default": 1
} }}