'How can I access each element in an array and use it for filtering data

I am trying to get an array in my req.query and want to use the elements in that array for filtering of data but unable to do so.

As you can see in the below code I am trying to apply conditional filters in a dynamic set of data. I am passing the views as an array in the req.query in which I want to search documents whose value lies between the first element and the second element of the array.

if someone can please help me to resolve this issue.

Please find below the relevant code


url applied to below get method : 

// http://localhost:3001/userfilterlist/check?views=[20,29]

router.get('/userfilterlist/check' , async (req , res) => {
    //console.log(req.query.views);
  
    let aggrQuery = [
        {
            '$match': {
                $and: [
                    req.query.views ? [{ 'views': { $gte: req.query.views[0], $lte: req.query.views[1] } }] : [],
                ]
            }
        }
    ]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source