我目前有以下代碼設定來從 MongoDB 資料庫中檢索一些資料,并且我想創建一個在收到的檔案上運行的 IF 函式。我試圖讓一個 IF 函式price在檔案中的欄位比event.payload.base_price其他欄位高 10% 時運行,我對編碼和 MongoDB 相當陌生,所以無法弄清楚這個,因為我不知道如何寫這個功能。任何幫助將不勝感激。
async function run() {
try {
const query = { contract: idSplit[1] , id: idSplit[2] };
const options = {
sort: { name: 1 },
projection: {_id: 0, slug: 1, contract: 1, id: 1, price: 1},
};
const cursor = listings.find(query, options);
await cursor.forEach(console.dir);
} finally {
await client.close();
}
}
run().catch(console.dir);
uj5u.com熱心網友回復:
得到陣列后的游標中,可以在 forEach 回圈中使用下面的 if 條件:
cursor.forEach((element) =>
{
if (element.price > event.payload.base_price event.payload.base_price * 0.1) {
console.log("Event is happening", element.price);
} else {
console.log("Event is not happening", element.price)
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/487252.html
標籤:javascript 数据库 mongodb
