简体   繁体   English

ZCCADCDEDB567ABAE643E15DCF0974E503Z 删除每个子文档中的数组项

[英]Mongoose delete array item in every subdocument

I have an User (click) Model with a cart where products are stored (just the id).我有一个用户(单击) Model,其中有一个存储产品的购物车(只是 id)。 If I want to delete a product from the Shop, the product should be also deleted from every users cart.如果我想从商店中删除产品,该产品也应该从每个用户购物车中删除。

Therefore I tried this code:因此我尝试了这段代码:

User.update({ $pull: {'cart.items': {productId: product._id }}});

Even if I want to delete the product from a specific User with findByIdAndUpdate, it doesn't work:即使我想使用 findByIdAndUpdate 从特定用户中删除产品,它也不起作用:

User.findByIdAndUpdate(user._id, { $pull: {'cart.items': {productId: product._id }}});

just try:试试看嘛:

User.update({},{ $pull: {'cart.items': {productId: product._id }}});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM