假设我在mongoose中有以下模型:
var Product = new Schema({
eanCode: String,
brandName: String,
productNameNl: String,
sex: String,
suggestedRetailPrice: Number
})
如何使用带参数的数组编写函数来查询此模型?我想要一个泛型函数,它可以使用一组过滤器参数从这个模型中获取任何东西。例如:
var filterArray = [
sex: "gents",
brand: "brandName"
];
var fieldsArray = ["sex", "brand", "productNameNl", "eanCode"];
var getBrandGentsProducts = getProducts(filterArray);
function getProducts(fields, filter){
Product.Find({fields}, {filter}).exec(function(err, products){
return products;
})
}