Answer the question
In order to leave comments, you need to log in
How to build a query url to pull data with multiple values in mongoose?
There is a query set like this:
{
category: 'motherboard',
'details.socket': 'lga1151v2',
'details.chipsets': 'z370,z390'
}
app.get('/products/', async (req, res) => {
try {
if (req.query.category) {
const category = await categoryModel.findOne({ name: req.query.category });
const products = await productModel.find({ ...req.query, category });
return res.status(200).json({ products });
}
const products = await productModel.find();
return res.status(200).json({ products });
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Something goes wrong!' });
}
});
Answer the question
In order to leave comments, you need to log in
Searching in arrays
https://docs.mongodb.com/manual/tutorial/query-arrays/
In your case most likely
productModel.find({‘details.chipsets’ : {$all:[‘z370’, ‘z390’ ]}});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question