【问题标题】:mongoose pull command to delete product from shopping cartmongoose pull 命令从购物车中删除产品
【发布时间】:2020-06-27 22:38:40
【问题描述】:
CartController.prototype.addToCart =(req, res, next) => {

    // Using mongoose in-built function "findbyID"
Product.findById({_id : req.params.productId}).then( item => 
{
    if (!item) {res.status(400).send({message : "item not found"})}
    Cart.findByIdAndUpdate(req.params.userId,
    {
        total_quantity : 0,
        total_price : 0,
        final_price : 0,
        "$push": {"products": {
                // Passing Data
                productid : item._id,
                MRP : item.offers.MRP,
                finalprice : item.offers.finalprice,
                discount :  item.offers.discount,
                organization : item.property.organization,
                brand : item.property.brand,
                color : item.property.color,
                weight : item.property.weight,
                category : item.property.category
            }
        },
        userid : req.params.userId   
    },
        { upsert: true, returnNewDocument : true}
    ).then(() => {
        res.status(200).send({message: "product added to cart"});
        }).catch(err => {
            res.status(500).send(err);
        });
}).catch (err => {
    res.status(500).send("item fetch related issue found", err);
});
};

//json输出

[
{
    "_id": "5e5a58843ed45a235c32ac8c",
    "__v": 0,
    "createdAt": "2020-03-16T18:04:31.370Z",
    "updatedAt": "2020-03-16T18:41:23.068Z",
    "userid": "5e5a58843ed45a235c32ac8c",
    "inCart": false,
    "products": [
        {
            "category": "Home Decor",
            "weight": 300,
            "color": "Multicolor",
            "MRP": 5000,
            "productid": "5e6f4234564e2d1b74ba3383",
            "_id": "5e6fbfaf3818775ac010187e",
            "quantity": 1,
            "brand": "",
            "organization": "",
            "discount": 20
        },
        {
            "category": "Home Decor",
            "weight": 300,
            "color": "Multicolor",
            "MRP": 5000,
            "productid": "5e6b21458f7019378c4981ff",
            "_id": "5e6fbff53818775ac0101881",
            "quantity": 1,
            "brand": "",
            "organization": "",
            "discount": 20
        }
    ],
    "final_price": 0,
    "total_price": 0,
    "total_quantity": 0
}
]

//购物车型号

    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;

var product = new Schema({
  productid : {
    type: Schema.Types.ObjectId,
    // required: true,
    ref: 'Product'
  },
  MRP : {
    type  : Number,
    // required : true
  },
  finalPrice : {
    type : Number
  },
  discount : {
    type : Number, default : 0
  },
  organization : {
    type  : String, default : null
  },
  brand : {
    type : String, default : null
  },
  color : {
    type : String
  },
  weight : {
    type : Number
  },
  category : {
    type : String,
    // required :true
  },
  quantity : {
    type : Number,
    // required : true,
    default : 1
  }
});

var cartSchema = new Schema({
  total_quantity : {type : Number, required : true, default: 0},
  total_price : {type : Number, required : true, default: 0},
  final_price : {type : Number, required : true, default: 0},
  products : [product],
  userid : { 
    type: Schema.Types.ObjectId,
    required: true,
    ref: 'Pujari'
  },
  inCart : {type : Boolean, default : 0}
  },
  {
    timestamps:true
  }
);

module.exports = mongoose.model('Cart', cartSchema);

//删除购物车商品

CartController.prototype.deleteCartItem = (req, res, next) => {
Cart.update({userid : req.params.userId}, { $pull: { products : {productid: req.params.productId }}}, {multi: true})
};

所以,基本上我必须为产品收集一个,为购物车收集另一个,我的问题是如何从购物车中删除特定产品,就像下面的 json 我必须产品一样。我只想删除 "productid": "5e6b21458f7019378c4981ff" 的对象,我尝试了 mongodb $pull 运算符,但它不适用于此数据。

【问题讨论】:

  • 我找到了docs.mongodb.com/manual/reference/operator/update/pull,但无法应用于此类数据
  • 请详细说明您希望移除的具体条件和时间。
  • 更新中的{$pull:{products:{"productid": "5e6f4234564e2d1b74ba3383"}}} 是否适合您?
  • @Joe 不,它不适用于此数据
  • @WernfriedDomscheit 我只想删除 "productid": "5e6b21458f7019378c4981ff" 的对象

标签: node.js json mongodb mongoose


【解决方案1】:

对我来说,您的更新正在工作(对于给定的文档):

db.collection.updateMany(
   { userid: "5e5a58843ed45a235c32ac8c" },
   { $pull: { "products": { productid: "5e6b21458f7019378c4981ff" } } }
)


{ 
    "acknowledged" : true, 
    "matchedCount" : 1.0, 
    "modifiedCount" : 1.0
}

【讨论】:

    【解决方案2】:

    终于找到了解决办法,我犯了一个小错误

    //delete cart item
    CartController.prototype.deleteCartItem = (req, res, next) => {
    Cart.update({userid : req.params.userId}, { $pull: { products : {category: "Home" }}}, function (err, data) {
        if (err) res.send(err)
        else res.send(data)
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-17
      • 2022-08-23
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多