I would like to update the servings, calories and nutrients in my mongo document. How would I be able to update the details?
{
"_id": ObjectId("id"),
"user_id": ObjectId("id"),
"date": 20170210,
"snacks": [
{
"nutrients": {
"protein": "0",
"carbs": "0",
"fat": "0"
},
"servings": 1,
"calories": 0.35,
"name": "Coca-Cola zero"
}
],
"dinner": [ ],
"lunch": [ ],
"breakfast": [ ]
}
Ive tried using $set however it hasnt not worked at all. This is what ive been trying:
user_food.update({user_id : req.session.user_id, date: 20170210, snacks:{name:"Coca-Cola zero"}}, {'$set':{'servings': 2}}, function(err, other){
if(err){
console.log("something went wrong: " + err);
return res.status(500).send(err);
}
else{
console.log("this is other: " + other);
return res.status(200).send(other);
}
});
SOLVED
The query that worked:
user_food.update({user_id : req.session.user_id, date: 20170210, 'snacks.name': food},{$set: {'snacks.$.servings': serving}},function(err, other){
if(err){
console.log("something went wrong: " + err);
return res.status(500).send(err);
}
else{
console.log("this is other: " + other);
return res.status(200).send(other);
}
});