mongoose联表查询与一般查询合并


var query=model.$.find({});
size=size/2;
query.skip((parseInt(page) - 1) * size);
query.limit(parseInt(size));
//只针对管理类任务
query.where({pm_type:'0'});
//根据任务名进行查询
if(condition.name && condition.name !==''){
query.where('pm_name',new RegExp(condition.name));
//选择开始时间年度为下拉列表默认值,当用户不设置值时,该字符串被传到这里,故需要处理掉
}if(condition.year && condition.year !==''&&condition.year!=='选择开始时间年度'){
query.where('pm_begin_date',new RegExp(condition.year));
//当前用户为发送者或者为接受者
}if(condition.current_id&&condition.current_id!==''){
query.where({$or: [ { pm_receivePerson: { $elemMatch: { id: condition.current_id } } },
{ 'pm_sendPersonID': condition.current_id}]});
//根据任务状态不同进行查询
}if(condition.status&&condition.status!==''){
query.where('pm_status').equals(condition.status);
//对查询数据进行排序
}if(sortItem.sort!==undefined&&sortItem.sort!==null&&sortItem.sort!==''&&sortItem.order!==undefined&&sortItem.order!==null&&sortItem.order!==''){
var so={};
so[sortItem.sort]=sortItem.order;
query.sort(so);
}
//计算分页数据
query.exec(function(err,rsForMan){
if(err){
cb(utils.returnMsg(false, '1000', '根据姓名查询出现异常。', null, err));
}else{
//计算数据总数,不限制也不跳过的。保证查询条件与上一致,只需要计算总的数据数量。
var queryForLength=model.$.find({});
queryForLength.where({pm_type:'0'});
if(condition.name && condition.name !==''){
queryForLength.where('pm_name',new RegExp(condition.name));
//选择开始时间年度为下拉列表默认值,当用户不设置值时,该字符串被传到这里,故需要处理掉
}if(condition.year && condition.year !==''&&condition.year!=='选择开始时间年度'){
queryForLength.where('pm_begin_date',new RegExp(condition.year));
//当前用户为发送者或者为接受者
}if(condition.current_id&&condition.current_id!==''){
queryForLength.where({$or: [ { pm_receivePerson: { $elemMatch: { id: condition.current_id } } },
{ 'pm_sendPersonID': condition.current_id}]});
}if(condition.status&&condition.status!==''){
queryForLength.where('pm_status').equals(condition.status);
}
queryForLength.exec(function(err,resultForManLen){
if(err){
cb(utils.returnMsg(false, '1000', '根据姓名查询出现异常。', null, err));
}else {
var queryN=model.$user_pm_statusModel.find({});
queryN.populate('pm_id');
queryN.skip((parseInt(page) - 1) * size);
queryN.limit(parseInt(size));
queryN.where({status:condition.status,user_id:condition.current_id});
if(sortItem.sort!==undefined&&sortItem.sort!==null&&sortItem.sort!==''&&sortItem.order!==undefined&&sortItem.order!==null&&sortItem.order!==''){
var so={};
so[sortItem.sort]=sortItem.order;
queryN.sort(so);
}
if(condition.name && condition.name !==''){
queryN.where('pm_id.pm_name',new RegExp(condition.name));
//选择开始时间年度为下拉列表默认值,当用户不设置值时,该字符串被传到这里,故需要处理掉
}if(condition.year && condition.year !==''&&condition.year!=='选择开始时间年度'){
queryN.where('pm_id.pm_begin_date',new RegExp(condition.year));
//当前用户为发送者或者为接受者
}

//计算分页数据
queryN.exec(function(err,rsForNot){
if(err){
cb(utils.returnMsg(false, '1000', '根据姓名查询出现异常。', null, err));
}else{
//计算数据总数

model.$user_pm_statusModel.find({status:condition.status,user_id:condition.current_id}).populate('pm_id').exec(function(err,resultForNotLen){
if(err){
cb(utils.returnMsg(false, '1000', '根据姓名查询出现异常。', null, err));
}else {
//合并数据
for(var i in rsForNot){
rsForMan.push(rsForNot[i].pm_id);
}

cb(utils.returnMsg4EasyuiPaging(true, '0000', '根据姓名查询成功。', rsForMan, resultForNotLen.length+resultForManLen.length));
}
});
}
});

}
});
}
});
posted @ 2017-08-28 10:04  Ysera_Dreamer  阅读(1030)  评论(0编辑  收藏  举报