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));                                }                            });                        }                    });                }            });        }    });
相關文章
相關標籤/搜索