[英]How to use dynamic condition with $match
我想每次都將數據與特定 ID 匹配
注意:項目 = ['id1','id2']
let logs = await Promise.all(
tpes.map(async (item) => {
return await this.logModel.aggregate([
{$match:{terminalId:item}},
{
$group: {
_id: '$outcome',
value: {
$sum: 1,
},
},
},
{
$project: {
name: '$_id',
value: 1,
_id: 0,
},
},
]);
}),
);
console.log('logs', logs);
return logs.flat();
我沒有 output,我很困惑,請問有什么解決方案或替代方案嗎?
無需在此處使用循環 - 如果要匹配terminalId
為id1
或id2
的所有記錄,則可以使用$in
運算符:
const terminalIds = ["id1", "id2"];
const result = await this.logModel.aggregate([
{$match:{terminalId:{$in: terminalIds}}},
// ... rest of your code
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.