簡體   English   中英

在貓鼬中過濾過濾結果

[英]Filter filtered results in Mongoose

我正在嘗試通過多個參數過濾模型。

首先,我找到那些名字包含查詢參數的對象:

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

如何完成過濾過濾結果?

聚合就是您要尋找的。 您的代碼看起來像(我沒有嘗試過貓鼬):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM