Javascript 猫鼬集团年度和月度业绩

Javascript 猫鼬集团年度和月度业绩,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,对于以下模式(伪代码) 我想返回按月份和年份分组的结果,类似于这样,同样是psuedo代码: [ { year: "2016", month: "4", results: [ {_id: "1232", title: "foo", content: "bar", date: "Some date" }, {_id: "1232", title: "foosdas", content: "barsdasda", date: "

对于以下模式(伪代码)

我想返回按月份和年份分组的结果,类似于这样,同样是psuedo代码:

 [
   { year: "2016", 
     month: "4", 
     results: [ 
         {_id: "1232", title: "foo", content: "bar", date: "Some date" }, 
         {_id: "1232", title: "foosdas", content: "barsdasda", date: "Some other date" } ] }
 ]
今天早上我一直在尝试Mongoose的聚合,它可以按月份和年份进行分组,但它不会返回实际的模型对象

这是我当前的聚合查询

               Post
                .aggregate({
                    $group: {
                        _id : { year: { $year : "$date" }, month: { $month : "$date" }},
                    }
                })
                .exec(function (error, items) {
                    if (error) { return next(error); }

                    console.log(items);
                    res.append("x-count", count);
                    return res.json("Done");
                });
你可以试试:

mongoose.model('yourCollection').find({year:'2016',month:'4'},function(err,results){
    //your code here
});
使用
$group
中的运算符定义
\u id
之外的字段

在本例中,您将与系统变量一起使用,以累积每月的文档数组:

Post.aggregate([{
    $group: {
        _id : { year: { $year : "$date" }, month: { $month : "$date" }},
        results: {$push: '$$ROOT'}
    }
}).exec(...

你能把你要去的地方和原始文件贴出来吗?谢谢!在那之后,在使用猫鼬之前我真的要三思。对于一个基本功能来说,这是一个多么大的解决方案啊。
Post.aggregate([{
    $group: {
        _id : { year: { $year : "$date" }, month: { $month : "$date" }},
        results: {$push: '$$ROOT'}
    }
}).exec(...