javascript Mongo find()查询不使用regexp过滤

mccptt67  于 2023-05-05  发布在  Java
关注(0)|答案(1)|浏览(81)
app.get("/expenses/:month", async (req, res) => {
    const { month } = req.params;
    const regexp = new RegExp("\d\d\d\d-" + month + "-\d\d");
    console.log(regexp);
    const allExpenses = await Expense.find({ date: { $regex: regexp } });
    const allEarnings = await Earning.find({ date: { $regex: regexp } });
    console.log(allExpenses);
    res.status(200).json({
        status: "success",
        results: allExpenses.length,
        data: {
            allExpenses,
            allEarnings
        }
    });

你好,我正在尝试使用regexp过滤出特定月份的数据。它查找没有regexp的数据,但是一旦我把它放进去,它就返回一个空数组。

rekjcdws

rekjcdws1#

我认为问题可能是你没有逃避反斜杠

const regexp = new RegExp("\\d\\d\\d\\d-" + month + "-\\d\\d");

相关问题