对于sql查询,我确实传递了一个参数并将其返回给回调,如下所示,我对SiteCode和BuildingName等所做的操作。如何通过Mongoose聚合实现类似的功能。
MSSQLmeterlogs.getMeterLogData(recordset[i].ID,fromDate,toDate,recordset[i].DisplaySiteCode,recordset[i].BuildingName,recordset[i].BuildingFloorArea,unit,(err,stcodert,stnamert,flrareart,logsrecordset) =>{
if(err){
return res.json({success:false,msg:'Error in retreiving logs.'});
}else{
}
});
module.exports.getMeterLogData = function(mtrid,stdate,enddate,DisplaySiteCode,BuildingName,BuildingFloorArea,unit,callback){
const request = new sql.Request();
var query = "Select * from employee;";
}
request.query(query, function (err, recordset) {
callback(err,DisplaySiteCode,BuildingName,BuildingFloorArea,recordset);
});
}
如何在猫鼬聚合中实现这一目标。
MeterLogsMongo.aggregate([
{
"$match": {
"MeterID" : mtrid,"LogDate": { $gte: startDate, $lt: endDate }
}
},
{
"$group": {
"_id": null,
"MeterID":{"$first" : "$MeterID"},
"Value": { "$sum" : "$Value"},
"Estimated" : {"$max" : "$Estimated"},
"NoSourceData" : {"$max" : "$NoSourceData"},
"count":{"$sum" : 1}
}
},
{
"$project":{'MeterID':1,'Value':{"$ifNull": [ "$Value", [{ "Value": 0 }] ]}
,'Estimated':1,'NoSourceData':1, '_id':0,'count':'$count',
'duration':{$multiply: [{$divide:[{$subtract: [endDate,startDate]}, 3600000]},2 ]},
'mtrid' : {$literal : mtrid},'sCode': {$literal : sitecode},'sName': {$literal : sitename},'fArea' : {$literal : floorarea}
}
},
{
"$project":{'MeterID':1,'Value':{"$ifNull": [ "$Value", [{ "Value": 0 }] ]},'Estimated':1,'NoSourceData':1, '_id':0,'isequal':{$eq :['$count','$duration']},
'mtrid':'$mtrid','sCode': '$sCode','sName':'$sName','fArea':'$fArea'}
},
{
"$match": {
"isequal": true
}
}
],function(err,logsrecordset) {
if(err){
return res.json({success:false,msg:'Error in retreiving logs.'});
}else{
}
});