这是我第一次看到对 node.js 和 mongodb 的愤怒,所以我可能做了一些愚蠢的事情或错过了一些明显的事情,但是鉴于下面的文档和查询,为什么会返回一个空对象?
1 文档示例
{
__v: 0,
_id: ObjectId("50bf7f12fc71b74f3b000001"),
amount: 452.23,
categoryId: 13,
created: ISODate("2012-12-05T17:06:26Z"),
description: "Test",
typeId: 1,
updated: ISODate("2012-12-05T17:06:26Z")
}
2 问题代码
var Schema = mongoose.Schema;
var Expense = new Schema({
description: { type: String, required: true },
amount: { type: Number, required: true },
created: { type: Date, default: Date.now },
updated: { type: Date, default: Date.now },
categoryId: { type: Number, required: true },
typeId: { type: Number, required: true }
});
var ExpenseModel = mongoose.model('Expense', Expense);
ExpenseModel.aggregate(
{ $group: { _id: '$typeId', total: { $sum: '$amount' }}},
function (err, summary) {
if(err){
return res.send(500, { error: err });
}
if(summary) {
return res.send(summary);
} else {
res.send(500, { error: 'couldnt find expenses' });
}
}
);