我想过滤我的产品。我的Mongodb中有4个产品。
这些是:
[
{name: "Dji Inspire 2", droneSeries: "inspire"},
{name: "Dji Phantom 4 Pro", droneSeries: "phantom"},
{name: "Dji Mavic 2 Pro", droneSeries: "mavic"},
{name: "Dji Spark", droneSeries: "spark"}
]
此外,我还有一个输入行(复选框(,用于选择要向您显示的无人机系列。这些是Inspire,Phantom,Mavic,Spark。
当我选中激励复选框时,我会看到激励无人机。在这里之前一切都很好。以下是寻找灵感无人机的代码:
const droneSeriesArray = await req.body.droneSeriesArray //["inspire"]
const droneSeriesString = await droneSeriesArray.toString() //inspire
let products = await Products.find({droneSeries: droneSeriesString})
但当我选中幻影复选框时,代码不起作用:
const droneSeriesArray = await req.body.droneSeriesArray //["inspire", "phantom"]
const droneSeriesString = await droneSeriesArray.toString() //inspire,phantom
let products = await Products.find({droneSeries: droneSeriesString})
我发现了这样的东西并尝试了一下:
const droneSeriesArray = await req.body.droneSeriesArray //["inspire", "phantom"]
const droneSeriesString = await droneSeriesArray.toString() //inspire,phantom
let products = await Products.find({droneSeries: {$elemMatch: {droneSeriesString}}})
这也不起作用。那么,你觉得呢?
您可以像下面的一样使用$in
await Products.find({droneSeries: {$in: droneSeriesArray}})