关于NodeJs 如何按条件分组查询,并返回结构
发布于 10 年前 作者 damonshen 4897 次浏览 最后一次编辑是 8 年前

需求如下: 现有History的Model有三个字段 :Content,Year,Month 数据: 1 “aaa”,2013,12 2 “bbb”,2013,11 3 “ccc”,2012,12 4 “ddd”,2011,03

想将数据按照Year来分组,保存到数组中,最后通过,res.render到具体页面 [{“aaa”,2013,} {“bbb”,2013,11}], [{“ccc”,2012,12}], [{“ddd”,2011,03}],

我的代码如下: index.js: var hisArrs=[]; History.distinct(“Year”,function(err,doc){ var years=doc; years.forEach(function(year){ History.find({“Year”:year},function(err,docs){ hisArrs.push(docs); }); }) ;
});

res.render(‘History/history’,{ title:‘history’, HisArrs:hisArrs }); 我代码思路大概是这样的,代码执行结果是不对的,请问应该怎样解决我的问题啊

2 回复

求解答啊,有木有遇到过啊。。。。。。

自己解决咯,哈哈哈

回到顶部