我的MongoDB(v4.2)管道由多个投影组成,用于减去,除,最高和乘以一个值。
有没有一种方法可以在不使用多个投影的情况下将它们组合在一起?
我的管道:
[
{
$project: {
duration: {
$subtract: [ '$updated', '$created' ]
}
}
},
{
$project: {
duration: {
$divide: [ '$duration', precision ]
}
}
},
{
$project: {
duration: {
$ceil: [ '$duration' ]
}
}
},
{
$project: {
duration: {
$multiply: [ '$duration', precision ]
}
}
},
{
$group: {
_id: '$duration',
count: {
$sum: 1
}
}
}
]
示例数据:
[
{ created: 1569763367, updated: 1569773367 },
{ created: 1569760000, updated: 1569770000 },
{ created: 1569772415, updated: 1569773519 },
]
答案 0 :(得分:1)
这似乎是可能的,但它不是很可读:
[
{
$project: {
duration: {
$multiply: [
{
$ceil: [
{
$divide: [
{
$subtract: [ '$updated', '$created' ]
},
precision
]
}
]
},
precision
]
}
}
},
{
$group: {
_id: '$duration',
count: {
$sum: 1
}
}
}
]