I am trying to receive multiple parameters without knowing which one arrives and which one does not, to filter a GET in the find () method
NODE JAVASCRIPT MONGOOSE
app.get('/venta', async (req, res) =>{
let a = req.query.anio;
let f = req.query.fecha;
let c = req.query.cajas;
let q = {};
if(a != undefined) q.anio = a;
if(f != undefined) q.fecha = f;
if(c != undefined) q.cajas = c;
Venta.find({$or: [q]}).exec((err,ventas)=>{
if(err) return res.status(400).json({
ok: false,
mensaje: 'no se encontro las ventas'
})
return res.json({
ok: true,
ventas
})
});
})
It occurred to me to put together a "q" list and insert the filters that are reached in the request. $ OR does not work
QUESTION> how can I fill the FIND () method based on the amount of filters that I am receiving in the request
thx
if you want any document that fulfill one of the conditions it needs to be a list of conditions. [q] is just an array with one element that contains all the filters with an and conjunction. to have the $or functionality it should be $or = [{anio:a},{fecha:f},{cajas:c}] for an arbitary number of filters you can loop on the key value pairs of req.query and insert them in an array accordingly. like in the example shown