L
L
lavezzi12020-02-24 07:24:04
MongoDB
lavezzi1, 2020-02-24 07:24:04

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'
}


Everything works except the last one. How to properly pull data with multiple values ​​for one filed? In my case details: { chipsets: [] } this array is in the base.

Route code:
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

1 answer(s)
A
Alexander Cheremkhin, 2020-02-29
@Che603000

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 question

Ask a Question

731 491 924 answers to any question