我正在尝试从猫鼬文档中存储的集合中删除一项。我的文档如下所示:
{ "__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" } ] }
我想删除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; } });
当我这样做时,出现错误“ TypeError:无法读取未定义的属性’等于’”。我不明白为什么。
您需要inventory.items.pull(req.params.itemSku),然后打个inventory.save电话。.remove用于顶级文档
inventory.items.pull(req.params.itemSku)
inventory.save
.remove