这是来自mongodb数据库的集合,我使用node调用API。首先,我想澄清一些事情: 第一个父ID被称为CategoryId,而在items数组中,该ID被称为ItemId。
{
"_id": "5c9dc6b29d30be2ac7a4539f",
"name": "Salads",
"description": "",
"isImage": 4,
"iCnt": 3,
"items": [
{
"_id": "5c9dcc17c4437434948b7d81",
"name": "Green Salad",
"description": "Freshly cut vegetables",
"price": 10,
"nonVeg": "No",
"icon_id": "",
"isSpecial": "No",
"topping_id": "",
"isImage": 0,
"type": "i",
"liked": 0,
"likes": 5
},
{
"_id": "5c9dc0b2fb23f429fe80adcc",
"name": "Brinjol & Cottage cheese salad",
"description": "Deep fried brinjol and cottage cheese",
"price": 15,
"nonVeg": "No",
"icon_id": "",
"isSpecial": "No",
"topping_id": "",
"isImage": 0,
"type": "i",
"liked": 0,
"likes": 6
}
]
}
我想更新_id: "5c9dcc17c4437434948b7d81"
(名称是Green Salad)数据,但是它使用Node.js代替了整个项目数组。有人可以帮忙吗?
itemUpdate = function (req, res) {
categoryModel.findByIdAndUpdate(req.params.id, { $set: req.body }, function (err, itemModel) {
if (err) return next(err);
res.send('item udpated.');
});
};
或
itemUpdate = function (req, res) {
({ "_id": req.params.categoryId, "items_id": req.params.itemId },
{ $pull: { "items._id": { "_id": req.params.id } } },
false,
true
);
}
这是我使用邮递员的req.body
:-
{
"name": "Chicken Soup",
"description": "yumee Chicken ",
"price": 10,
"nonVeg": "Yes",
"icon_id": "",
"isSpecial": "No",
"topping_id": "",
"isImage": 0,
"type": "i",
"liked": 0,
"likes": 5,
"categoryId":"5c9dc6b29d30be2ac7a4539f",
"itemId":"5c9dcc17c4437434948b7d81"
}