【问题标题】:Remove a object from array using MongoDB使用 MongoDB 从数组中删除对象
【发布时间】:2021-12-26 19:06:55
【问题描述】:

当我从网络上删除产品时,我试图从用户的购物车中删除它,但我不知道该怎么做。

User的moongoose架构如下:

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' }
          }]
      }
});

我想从 cartItems 中删除一个项目。我该怎么做?

【问题讨论】:

    标签: javascript mongodb


    【解决方案1】:
    db.collection.update(
      { _id: id },
      { $pull: { 'cartItems.type': { product: '....' } } }
    );
    

    更多关于$pull

    【讨论】:

      猜你喜欢
      • 2013-03-16
      • 2023-02-15
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 2016-03-16
      相关资源
      最近更新 更多