Javascript 聚合多个匹配项
Javascript aggregate with multiple matches
我正在尝试创建一个包含多个匹配项的聚合管道,但不确定该怎么做?!?
这是我正在尝试的:
const pipeline = [
{ match: { oomId: oomId, holes: 0, collectPutts: true } },
{ group: {
objectId: '$userId',
puttsPlayed: { $sum: 1 },
puts: {$sum: '$puts'},
gir: {$sum: '$gir'},
girHcp: {$sum: '$girHcp'}
} },
{ match: { oomId: oomId, holes: 0, collectHits: true } },
{ group: {
objectId: '$userId',
hits: {$sum: '$hit'},
holesHit: {$sum: '$holesToHit'}
} },
{ match: { oomId: oomId, holes: 0 },
{ group: {
objectId: '$userId',
totalPlayed: { $sum: 1 },
points: {$sum: '$points'},
strokes: {$sum: '$strokes'}
} },
{ project:{
played: '$totalPlayed',
puttsPlayed: '$puttsPlayed',
puts: {$divide: ['$puts', '$puttsPlayed']},
gir: {$divide: ['$gir', '$puttsPlayed']},
girHcp: {$divide: ['$girHcp', '$puttsPlayed']},
hits: '$hits',
holesHit: '$holesHit',
hitsPercentage: {
$multiply: [
{ $divide: [ "$hits", "$holesHit" ] },
100
]
},
points: {$divide: ['$points', '$totalPlayed']},
strokes: {$divide: ['$strokes', '$totalPlayed']}
}}
];
我收到此错误:意外标记“{”
不确定错误在哪里,甚至可以做我想做的事吗?还是我应该使用 3 个不同的管道?
感谢任何帮助...新聚合:-/
您在第 3 $match
阶段后没有关闭括号。所以,而不是这个:
{ match: { oomId: oomId, holes: 0 },
这样做:
{ match: { oomId: oomId, holes: 0 } },
我正在尝试创建一个包含多个匹配项的聚合管道,但不确定该怎么做?!?
这是我正在尝试的:
const pipeline = [
{ match: { oomId: oomId, holes: 0, collectPutts: true } },
{ group: {
objectId: '$userId',
puttsPlayed: { $sum: 1 },
puts: {$sum: '$puts'},
gir: {$sum: '$gir'},
girHcp: {$sum: '$girHcp'}
} },
{ match: { oomId: oomId, holes: 0, collectHits: true } },
{ group: {
objectId: '$userId',
hits: {$sum: '$hit'},
holesHit: {$sum: '$holesToHit'}
} },
{ match: { oomId: oomId, holes: 0 },
{ group: {
objectId: '$userId',
totalPlayed: { $sum: 1 },
points: {$sum: '$points'},
strokes: {$sum: '$strokes'}
} },
{ project:{
played: '$totalPlayed',
puttsPlayed: '$puttsPlayed',
puts: {$divide: ['$puts', '$puttsPlayed']},
gir: {$divide: ['$gir', '$puttsPlayed']},
girHcp: {$divide: ['$girHcp', '$puttsPlayed']},
hits: '$hits',
holesHit: '$holesHit',
hitsPercentage: {
$multiply: [
{ $divide: [ "$hits", "$holesHit" ] },
100
]
},
points: {$divide: ['$points', '$totalPlayed']},
strokes: {$divide: ['$strokes', '$totalPlayed']}
}}
];
我收到此错误:意外标记“{”
不确定错误在哪里,甚至可以做我想做的事吗?还是我应该使用 3 个不同的管道?
感谢任何帮助...新聚合:-/
您在第 3 $match
阶段后没有关闭括号。所以,而不是这个:
{ match: { oomId: oomId, holes: 0 },
这样做:
{ match: { oomId: oomId, holes: 0 } },