node.js 使用 mongoose 从 Mongo 中删除子文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18553946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove sub-document from Mongo with mongoose
提问by Brandon
I am trying to remove an item from a collection that is stored in a mongoose document. My document looks like this:
我正在尝试从存储在 mongoose 文档中的集合中删除一个项目。我的文档是这样的:
{
"__v": 3,
"_id": "5221040475f174d59a000005",
"items": [
{
"sku": 1321654654613213,
"name": "goldfish",
"quantity": 12,
"_id": "52224ed5bd9d340000000003"
},
{
"sku": 12,
"name": "goldfish",
"quantity": 13,
"_id": "52225dcbf2f1e40000000003"
},
{
"sku": 1299,
"name": "goldfish",
"quantity": 13,
"_id": "522260b6f2f1e40000000004"
}
]
}
I want to remove the goldfish with the sku of 12. I am doing the following:
我想删除sku为12的金鱼。我正在执行以下操作:
var inventory = res.locals.content;
inventory.items.remove( {sku: req.params.itemSku}, function (err, item) {
if (err) {
console.log('error occurred', err);
res.send('error');
}
else {
res.send('Item found and deleted');
return;
}
});
when I do this, I get the error "TypeError: Cannot read property 'equals' of undefined". I don't understand why.
当我这样做时,我收到错误“TypeError:无法读取未定义的属性‘等于’”。我不明白为什么。
回答by Alex
回答by Peter Lyons
You want inventory.items.pull(req.params.itemSku), followed by an inventory.savecall. .removeis for top-level documents
你想要inventory.items.pull(req.params.itemSku),然后是一个inventory.save电话。.remove用于顶级文档
回答by Stan Smith
finaly!
终于!
MongoDB:
"imgs" : {"other" : [ {
"crop" : "../uploads/584251f58148e3150fa5c1a7/photo_2016-11-09_21-38-55.jpg",
"origin" : "../uploads/584251f58148e3150fa5c1a7/o-photo_2016-11-09_21-38-55.jpg",
"_id" : ObjectId("58433bdcf75adf27cb1e8608")
}
]
},
router.get('/obj/:id', function(req, res) {
var id = req.params.id;
Model.findOne({'imgs.other._id': id}, function (err, result) {
result.imgs.other.id(id).remove();
result.save();
});
回答by robertfoenix
Removing a subdocument from an array
从数组中删除子文档
The nicest and most complete solution I have found that both finds and removes a subdocument from an arrayis using Mongoose's $pullmethod:
我发现从数组中查找和删除子文档的最好和最完整的解决方案是使用 Mongoose 的$pull方法:
Collection.findOneAndUpdate(
{ _id: yourCollectionId },
{ $pull: { subdocumentsArray: { _id: subdocumentId} } },
{ new: true },
function(err) {
if (err) { console.log(err) }
}
)
The {new: true}also ensures the existing document is overwritten.
本{new: true}还确保现有的文档将被覆盖。
回答by marco antonio cobian avalos
const deleteitem = (req, res) => {
var id = req.body.id
var iditem = req.body.iditem
Venta.findOne({'_id': id}, function(err,result){
if (err) {
console.log(err);
}else{
result.items.pull(iditem)
result.save()
}
})}
module.exports = {deleteitem }
回答by Syeda Aimen Batool
You can simply use $pullto remove a sub-document.
您可以简单地使用$pull删除子文档。
Collection.update({
_id: parentDocumentId
}, {
$pull: {
subDocument: {
_id: SubDocumentId
}
}
});
This will find your parent document against given ID and then will remove the element from subDocument which matched the given criteria.
这将根据给定的 ID 找到您的父文档,然后从子文档中删除与给定条件匹配的元素。

