直接上代码
db.collection('t_task')
.aggregate()
.match({enabled: 1})
.bucket({
groupBy: '$type',
boundaries: [0, 1, 2, 3, 4],
output: {
tasks: $.push({
_id:'$_id',
type: "$type",
name: "$name",
remark: "$remark",
date: '$date',
time: '$time',
isWarn: '$isWarn',
deadline: '$deadline',
})
}
}).end({
success: res => {
console.log('getTask res: ', res)
},
fail: err => {
console.error('getTask err: ', err)
}
代码很简单,需要注意的点在于,返回结果需要将返回的字段push到json对象里即可。参数说明可自行查询文档: