简体   繁体   中英

Mongoose delete array item in every subdocument

I have an User (click) Model with a cart where products are stored (just the 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:

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

just try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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