gpt4 book ai didi

javascript - 在 Mongoose 中过滤过滤结果

转载 作者:行者123 更新时间:2023-12-02 14:37:34 26 4
gpt4 key购买 nike

我正在尝试通过多个参数过滤模型。

首先,我找到那些名字包含查询参数的对象:

if (firstName){
User.find({firstName: new RegExp(firstName, "i")}, function (err, users) {
filteredUsers = users;
});
}

然后,我想继续过滤filteredUsers,例如:

if (lastName){
filteredUsers.find({firstName: new RegExp(lastName, "i")}, function (err, users) {
filteredUsers = users;
})
}

等等

但是,我自然会收到此错误:

 #<Object> is not a function

如何完成过滤过滤结果?

最佳答案

aggregation就是您正在寻找的。你的代码看起来像(我没有尝试过 Mongoose ):

Users.aggregate(
[
{ $match: firstname ? { firstname: new RegExp(firstname, "i") } : {} },
{ $match: lastname ? { lastname: new RegExp(lastname, "i") } : {} }
]
);

关于javascript - 在 Mongoose 中过滤过滤结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37309537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com