我正在尝试编写一个节点API,它将根据在API调用中传递的过滤器来获取产品列表。Form&lt&gt查询猫鼬
如:http://localhost:8080/api/products?price=> 100 &价格= 的color = red
我已经写了下面的代码来获取所有的产品。
Product.find().populate("images", "image").exec(function(err, products){
if(err){
res.status(200);
return res.json({
success: false,
message: err,
});
}else{
res.status(200);
return res.json({
success: true,
message: 'Products fetched successfully',
data : products
});
}
});
我应该如何更改上述代码以使用API调用中传递的任何过滤器?我知道如果我将{$and:[{price:{ $gt: 100, $lt: 200 }},{colour:red}]}
作为我给出的上述示例,我会得到期望的结果。
如何形成查询动态?
重命名你的URL参数'minPrice'和'maxPrice',其余的将水到渠成。 – Tomalak