0
造成的錯誤,我已經用我的本地託管的發展中減少功能,但是現在我已經部署的代碼到一個活的服務器和IM現在得到一個錯誤:使用
MongoError: exception: invalid operator '$reduce'
IM電影讓每天的食物日記,計算消耗的總熱量:
user_food.aggregate({$match:{user_id:mongoose.Types.ObjectId(req.session.user_id)}},{$project: {date:1,calories:{$add:[
{$reduce: {input: "$breakfast",initialValue: 0,in: { $add : ["$$value", "$$this.calories"]}}},
{$reduce: {input: "$lunch",initialValue: 0,in: { $add : ["$$value", "$$this.calories"]}}},
{$reduce: {input: "$snacks",initialValue: 0,in: { $add : ["$$value", "$$this.calories"]}}},
{$reduce: {input: "$dinner",initialValue: 0,in: { $add : ["$$value", "$$this.calories"]}}}]}}}, function (err, results) {
//if there is an error
if (err) {
console.log("something went wrong: " + err);
return res.status(500).send(err);
} else {
return res.status(200).send(results);
}
});
用戶食物的文檔是非常簡單的:
{
"_id": ObjectId("58d26c3dd60bd36f40fa0498"),
"user_id": ObjectId("587cd30bd338b90a6c18e58f"),
"calories": 2000,
"date": 20170222,
"snacks": [
{
"nutrients": {
"protein": 7.5,
"carbs": 56.5,
"fat": 10.5
},
"servings": 23.8,
"calories": 526,
"name": "Dairy milk"
}
],
"dinner": [ ],
"lunch": [
{
"nutrients": {
"protein": 9.5,
"carbs": 25.4,
"fat": 17.2
},
"servings": 60,
"calories": 751,
"name": "2 pork sausages rolls"
}
],
"breakfast": [
{
"nutrients": {
"protein": "4.8",
"carbs": "65",
"fat": "26"
},
"servings": 20.5,
"calories": 438,
"name": "Oreo"
}
],
"__v": 0
}
如果有人能指出我正確的方向來解決這個ID是非常感謝。
$減少是在3.4版本新。你可以通過在我們的mongo shell中執行db.version()來檢查你的db版本嗎? –
@Blastfreak的2.6.12版本,有沒有解決這個問題的方法可以在舊版本上運行? –
基本上它是在3.4中引入的,所以最好和穩定的解決方法是升級.. –