我正在嘗試使用 mongoose 模型從 mongodb 獲取客戶和商店,
getCustomer 函式引數是customer 和store,但有時store 引數為null || 不明確的
// Mongoose Model
const Customer = require("../models")
// Find customer && Store
const getCustomer = async (customer, store) => {
const result = await Customer.aggregate([
{
$match: {
customerId: customer.id,
storeId: store.id,
},
},
]);
return result;
};
所以這個 $match 查詢總的來說是回傳錯誤,因為沒有找到 store.id。
如果無條件傳遞存盤資料,你能告訴我應該使用什么查詢嗎?
uj5u.com熱心網友回復:
有條件地傳遞store.id,如下所示:
const getCustomer = async (customer, store) => {
const result = await Customer.aggregate([
{
$match: {
customerId: customer.id,
...(store && store.id ? { storeId: store.id } : {})
},
},
]);
return result;
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/535608.html
標籤:节点.js数据库猫鼬
