我需要以下输出的代码。我尝试了somany时间但未能找到。 这是我想要的输出格式
{
name: "Jan",
ds10: 0,
ds9: 0,
ds8: 0,
ds7: 0,
ds6: 0,
ds5: 0,
ds4: 0,
ds3: 0,
ds2: 0,
ds1: 0
},
这是一个代码,用于查找2004年至2013年间的月份数。 ds1到ds10的发生是两年的差异。如果我们增加年份,“ds”范围就会根据这个差异而来。 那是
ds1: corresponding to 2004
ds2: corresponding to 2005
ds3: corresponding to 2006
这只是1月份的一个例子。
我写了这样的代码。
app.get('/stats-by-time', function(req, res){
var year = (req.query["year"]) ? req.query["year"] : "";
var year2 = (req.query["year2"]) ? req.query["year2"] : "";
var family = (req.query["family"]) ? req.query["family"] : "";
var month = (req.query["month"]) ? req.query["month"] : "";
var eventDate = (req.query["eventDate"]) ? req.query["eventDate"] : "";
var errFlag = 1;
var errObj = '';
var filter = (req.query["filter"]) ? req.query["filter"] : "";
//var filter = req.query["filter"] || {};
try {
filter = (filter != '') ? JSON.parse(filter) : {};
} catch(err) {
errObj = err;
errFlag = 0;
}
var callback = req.query["callback"] || '';
var yearvalues = {};
yearvalues["year"] = year;
if(year !="year" && year2 != ''){
var objyears = { $and: [ { "year" : { $gte: year } }, { "year": { $lte: year2 } } , filter ] }
collection.aggregate(
{ $match : objyears},
{ $group : {
_id : '$month',
//month : '$month',
count : { $sum : 1 },
}},
{ $sort : {_id: 1}},
function(err, results) {
var outputArray = [];
for(var j in results) {
var obj1 = {};
var objyear = results[j];
obj1["name"] = objyear['_id'];
//obj1["ds"] = ""+ objyear['count'] +"";
obj1["ds"] = objyear['count'];
var diff = year2 - year;
console.log("difference",diff);
outputArray.push(obj1);
}
console.log("results",outputArray);
res.writeHead(200, {'Content-Type': 'application/json'});
if (callback != '') {
res.end(callback + '(' + JSON.stringify({success: true, rows: outputArray}) + ')');
} else {
res.end(JSON.stringify({success: true, rows: outputArray}));
}
});
} else {
res.end(JSON.stringify({success: false, error:{msg:"Year2 must be passed"}}));
}
});