Skip to content

Instantly share code, notes, and snippets.

@DogeVenci
Created January 7, 2019 14:23
Show Gist options
  • Save DogeVenci/e215891d7bf00c9b80ae37baadd51f8a to your computer and use it in GitHub Desktop.
Save DogeVenci/e215891d7bf00c9b80ae37baadd51f8a to your computer and use it in GitHub Desktop.
mongo 分段聚合查询 字符串转int
db.getCollection('xxx').aggregate([
{
$project: {
_id: 0,
mage: { $toInt: "$age" //age 字符串类型转int比较过滤
}
}
},
{
$match: {
'mage': {
"$exists": true,
"$ne": null //过滤为null的
}
}
},
{
$bucket: {
groupBy: "$mage", // 对过滤后的mage字段进行统计查询
boundaries: [ 0, 18, 20, 25, 30, 35, 40, 45, 50], // 数据的边界范围,包左不包右
default: "50+", // 不在boundaries内的默认名称
output: {
"count": { $sum: 1
}, // 统计
}
}
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment