【发布时间】:2016-04-11 21:15:17
【问题描述】:
对于以下架构(伪代码)。
var Post = {
title: "",
content: "",
date: new Date()
}
我想返回按月和年分组的结果,所以像这样,再次伪代码:
[
{ 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");
});
【问题讨论】:
-
你能把你去的地方和原始文件吗?
标签: javascript node.js mongodb