首页 > 解决方案 > 无法同时过滤查找方法和填充方法中的嵌套记录

问题描述

我正在尝试根据我的 searchKey 过滤掉嵌套记录。例如:患者 - >提供者想要根据 searchKey 一起搜索患者和提供者。

但是,当我使用患者详细信息触发 searchkey 时,它正在工作,但是当我输入与提供者相关的任何关键字(想要搜索提供者)但这次过滤器不起作用时。这是我的代码:

patients = await Patient.find({
                    $or: [
                        { firstName: { $regex: searchKey, $options: "i" } },
                        { lastName: { $regex: searchKey, $options: "i" } },
                        { socialSecurityNumber: { $regex: searchKey, $options: "i" } },
                    ],
                    provider: currentUser,
                }).populate({ path: "provider", select: "firstName lastName",  match : {
                        $or: [
                                {lastName: { $regex: searchKey,$options: "i" }},
                                {firstName: { $regex: searchKey,$options: "i" }},
                            ]
                    } })
                    .populate({ path: "gender", select: "name -_id" })
                    .skip(pageSize * pageNumber - pageSize)
                    .limit(pageSize).lean();

标签: javascriptnode.jsmongodbmongoosemongoose-populate

解决方案


推荐阅读