-1
I would like to know how I can search between dates.
Example:
Bring all data between 06/12/2018 to 06/11/2019.
Bring all data for a given year or month.
How can I make these filters in my controller with nodejs and mongodb?
I tried it as follows but I did not succeed, it follows the index method of the Pedidocontroller class:
async index(req, res) {
const filters = {};
// filtro de pesquisa
if (req.query.data_min) {
filters.createdAt.$gte = req.query.data_min;
}
const userLogado = await User.findById(req.userId);
// se não for um provedor
if (userLogado.provedor !== true) {
const pedidos = await Pedido.find({
cliente: req.userId
}).populate("cliente");
return res.json(pedidos);
}
const pedidos = await Pedido.paginate(filters, {
page: req.params.id,
limit: 10,
populate: ["cliente"],
sort: "-createdAt"
});
return res.json(pedidos);
}
has some error message?
– Marcelo Vismari
No, it returns normal as if n had the filter..
– Junior Marques
Try to convert the value
req.params.data_min
for the guyDate
– Marcelo Vismari
@Marcelovismari tried this but without success, I think it’s because my createdAt is saved in Datetime format (2019-12-24T16:42:11.669Z) in the database.
– Junior Marques