如果请求带有startDate endDate查询,则尝试匹配日期。但是我正在使用以下方法返回空数据。
try {
console.log(req.query.startDate);
console.log(new Date(req.query.startDate));
console.log(new Date(req.query.startDate).toISOString());
let match = {};
if (req.query.userId) {
match.userId = req.query.userId;
}
if (req.query.startDate && req.query.endDate) {
match.createdAt = {
$gte: new Date(req.query.startDate),
$lt: new Date(req.query.endDate)
};
}
const docs = await Sale.aggregate([{
$match: match
}
]);
return res.status(200).json({
docs
});
}
From console log:
2019-01-01
2019-01-01T00:00:00.000Z
2019-01-01T00:00:00.000Z