到目前为止,我只能设法删除第一个ID(在本例中为" 12345")。
我尝试在id
- 数组
books
2行
Libary Table:
{
"_id": {
"$oid": "12345"
},
"libaryName": "A random libary",
"Books": [
{
"_id": {
"$oid": "1"
}
"bookTitle": "Example",
"TotalPages": "500"
},
{
"_id": {
"$oid": "2"
}
"bookTitle": "Delete Me",
"TotalPages": "400"
}
]
}
我的删除代码:
router.delete('/:id', (req, res) => {
Libary.remove({ _id: req.params.id })
.then(() => {
//redirect
});
});
如何访问和删除id
为2的图书行?
答案 0 :(得分:2)
您需要使用Library.update(
{ },
{ $pull: { Books: { _id: 2 } } }
)
:
function formatData(response) {
let newFormat = {
datasets: [{
data: []
}],
labels: []
};
response.result.data.forEach(item => {
newFormat.datasets[0].data.push(item.clicks);
newFormat.labels.push(item.label);
});
return newFormat;
}
答案 1 :(得分:2)
您需要使用$pull opertator
router.delete('/:id', (req, res) => {
Libary.update({ _id: req.params.id }, //This is the Id of library Document
{ $pull: { "Books": {"_id":2) } } }) // This will be the Id of book to be deleted
.then(() => {
//redirect
});
});
希望它有所帮助。