node.js - 如何在 find() 上链接 Mongoose model.find()
问题描述
这是在 Node.js 方法中分两步过滤 mongoDb 集合的方法。我正在使用猫鼬。
步骤 1:首先根据用户的角色过滤集合
步骤2:在步骤1的过滤结果上,根据搜索字符串进一步过滤和/或选择前端选择的选项。
下面是编写的代码,但在 .find() 查询结果上应用 .find() 作为链不起作用。每当应用第二步时,过滤后的结果不符合第一步。
请提出最佳替代方案
async function getServiceLog(fy, filter, sortKey, sortOrder, pageSize, pageIndex, filtObj, user){
let fetchedDocs;
let docQuery;
//// STEP 1
if(user.role === 'Admin' || user.managesAll === true) {
// display all documents
docQuery = ServiceLog.find({FY: fy}).collation({ locale: 'en'});
} else if(user.isManager === true) {
// display documents where execName = userName or approver = userName or execName is in user.team
docQuery = ServiceLog.find({
$and: [
{ FY: fy},
{
$or: [
{ execName: user.userName },
{ approver: user.userName },
{ rm: { $regex: user.userName, $options: 'i' } },
{ execName: { $in: user.team.map(u => u.userName) } },
]
}
]
});
} else if(!user.isManager || user.isManager === false) {
// display documents where execName = userName or approver = userName
docQuery = ServiceLog.find({
$and: [
{ FY: fy},
{
$or: [
{ execName: user.userName },
{ approver: user.userName },
{ rm: { $regex: user.userName, $options: 'i' } },
],
}
]
});
}
//// STEP 2
let filtType = 'none';
if(filter && filtObj.length <= 0) {
filtType = 'filter'
} else if(!filter && filtObj.length > 0) {
filtType = 'filtObj'
} else if(filter && filtObj.length > 0) {
filtType = 'both'
}
switch(filtType) {
case "none":
break;
case "filter":
docQuery = docQuery.find({ // <<<< Second .find()
$or: [
{ compName: { $regex: filter, $options: 'i' } },
{ servName: { $regex: filter, $options: 'i' } },
],
}).collation({ locale: 'en'});
break;
case "filtObj":
docQuery = docQuery.find({ // <<<< Second .find()
$and: filtObj
}).collation({ locale: 'en'});
break;
case "both":
docQuery = docQuery.find({ // <<<< Second .find()
$and: [
{
$or: [
{ compName: { $regex: filter, $options: 'i' } },
{ servName: { $regex: filter, $options: 'i' } },
],
},
{
$and: filtObj
}
]
}).collation({ locale: 'en'});
break;
}
/// STEP 3: Finalize the result by applying sort and pagination
///// this is to find out the count of documents matching the search/filter criteria
//// as later this will get trimmed due to page size
fetchedDocs = await docQuery;
if(!fetchedDocs) fetchedDocs = [];
totalCount = fetchedDocs.length;
if(!totalCount) totalCount = 0;
if(totalCount > 0) {
if(pageSize && pageIndex >= 0) {
if(!sortKey) {
docQuery.skip(pageSize * pageIndex).limit(pageSize);
}else{
if(sortOrder === 'desc') {
sortOption = '-' + sortKey
}else {
sortOption = sortKey
}
docQuery.sort(sortOption).skip(pageSize * pageIndex).limit(pageSize);
}
}
fetchedDocs = await docQuery;
}
if(!fetchedDocs) fetchedDocs = [];
servLogData = {
servicelogs: fetchedDocs,
totalCount: totalCount
}
return servLogData;
}
解决方案
推荐阅读
- java - 在jsp中显示错误登录消息
- dropbox-api - Dropbox Python API - Upload function
- git - Git缺少存储库上的文件和文件夹
- d3.js - D3 Error:
attribute d: Expected number, "MNaN,NaNLNaN,NaNL…" - c# - 操作子框架内的控件的最佳方法是什么?(UWP)
- python - 如何创建一个漂亮的一维图?
- ios - UIPickerView 委托未在子视图中调用
- jestjs - 开玩笑 - 在每次测试之前做一些事情
- istio - Istio 0.8。跟踪请求中应用的 istio 对象和规则
- reactjs - 作为 React 道具传递的函数未出现在被调用的子项中