我正在尝试使用基于猫鼬模型的聚合。我想根据categoryId合计金额。但当试图通过路由url访问时,我遇到了这个错误。(对于路径“_id”处的值“test”,强制转换为ObjectId失败)。
我的猫鼬模型在测试中。js(型号)
var ObjectId = Schema.ObjectId;
var test = new Schema({
//testId:ObjectId,
testName: String,
amount:Number,
categoryId:String
});
module.exports = mongoose.model('test', test);
我的控制器测试控制器-
var Test = require('../models/test');
function aggTest(req,res){
Test.aggregate([{
$group:{
_id:"$categoryId",
totalAmount:{$sum: "$amount"}
}
}],function(err,result){
console.log(result);
});
}
路线映射
router.get('/test',testController.aggTest);
错误是-
{
message: "Cast to ObjectId failed for value "test" at path "_id""
name: "CastError"
type: "ObjectId"
value: "test"
path: "_id"
}
有什么建议吗?