简体   繁体   English

使用 MongoDB 从阵列中删除 object

[英]Remove a object from array using MongoDB

I am trying to remove a product from the user's cart when I delete it from the web, but I don't know how to do it.当我从 web 中删除产品时,我试图从用户的购物车中删除产品,但我不知道该怎么做。

The moongoose schema of User is the following:用户的月鹅模式如下:

var schema = mongoose.Schema({
      email: { type: String, required: true },
      password: { type: String, required: true },
      name: { type: String, required: true },
      surname: { type: String, required: true },
      address: { type: String, required: true },
      birth: { type: Date, required: true },
      cartItems: {
          type: [{
              qty: { type: Number, required: true },
              product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product' }
          }]
      }
});

I want to remove an item from cartItems.我想从 cartItems 中删除一个项目。 How can I do it?我该怎么做?

db.collection.update(
  { _id: id },
  { $pull: { 'cartItems.type': { product: '....' } } }
);

more about$pull更多关于$pull

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

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