从填充一些数据并在获取的字段中搜索获取数据后,如何进行分页?

时间:2019-08-08 07:30:55

标签: node.js mongodb mongoose mongoose-populate

所以我需要的是在填充字段的搜索字段中得到空结果之后,在分页中不提供任何数据并且总页数为0。

当我们无法搜索填充字段时,它还会显示具有填充值为空值的字段的数据条目。

当firstName或lastName上的匹配不匹配时,它将显示填充字段(nationWideAdmin)的空值。 我想显示分页数据计数= 0或值计数为null的值。即仅计算存在全国控制台数据的值。

{
"message": "",
"data": {
    "docs": [
        {
            "role": "NATIONWIDEADMIN",
            "accessToPortal": [],
            "_id": "5d4bbf2b0447315e7b4c6e63",
            "createdBy": "5d4a708f9f99c8f2f395408a",
            "updatedBy": "5d4a708f9f99c8f2f395408a",
            "nationWideAdmin": null,
            "email": "anuj@annnuj.com",
            "createdAt": "2019-08-08T06:20:27.227Z",
            "updatedAt": "2019-08-08T06:20:27.227Z",
            "__v": 0
        },
        {
            "role": "NATIONWIDEADMIN",
            "accessToPortal": [],
            "_id": "5d4adacbb098a53091223e14",
            "createdBy": "5d4a708f9f99c8f2f395408a",
            "updatedBy": "5d4a708f9f99c8f2f395408a",
            "nationWideAdmin": null,
            "email": "anuj@annnuj.com",
            "createdAt": "2019-08-07T14:06:03.150Z",
            "updatedAt": "2019-08-07T14:06:03.150Z",
            "__v": 0
        }
    ],
    "total": 2,
    "limit": 10,
    "page": 1,
    "pages": 1
},
"error": null

}

///这是我在查询中使用的内容。

static async getNationwideAdmin({ pageNo, limit, search }) {
        try {
            const query = {};
            search = search || "";
            const options = {
                "select": {password:0},
                "page": pageNo,
                "limit": limit,
                "sort": { "createdAt": -1 },
                populate: {
                    path: "nationWideAdmin",
                    select: "businessAddress firstName lastName",
                    match: { $or: [{ firstName: { $regex: new 
    RegExp(`.*${search}.*`, "i") } }, { lastName: { $regex: new 
    RegExp(`.*${search}.*`, "i") } }] }
                },

            };
            let result = await nationWideAdminModel.paginate(query, 
   options)
            return result;
        }
        catch (error) {
            throw error;
        }
   }  `

0 个答案:

没有答案