【问题标题】:How to delete a item in model respect to the id using mongoose and nodejs如何使用猫鼬和nodejs删除模型中关于id的项目
【发布时间】:2016-06-22 01:09:22
【问题描述】:

这是我的架构

var purchaseOrderModel = function () {
    var itemSchema = mongoose.Schema({
        strBarCode: {type: String },
        strItemName: {type: String },
        strQuantity: {type: String }
    });

    var purchaseOrderSchema = mongoose.Schema({
        strSupplierID: {type: String, required: true },
        strInvoiceNo: {type: String,required: true },       
        dteInvoiceDate: {type: Date, required: true },

        items: [itemSchema],

        created_by: { type: mongoose.Schema.Types.ObjectId, ref: 'user' }
    });



    return mongoose.model('purchaseorder', purchaseOrderSchema);

};

PurchaseOrder.find({items._id:req.params.itemid}, function (err, items) {
    return items.remove(function (err) {
    });         

});

如何删除 purchaseOrderSchema 中相对于 id 的项目。 项目使用 itemSchema 存储。

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    您可以使用 $pull 更新运算符从数组中删除项目,如下所示:

    PurchaseOrder.update(
        { "items._id": req.params.itemid },
        {
            "$pull": {
                "items": {"_id": req.params.itemid}
            }
        },
        function (err, doc) {
            if(err) { /* handle err */}
            console.log(doc);
        }
    );
    

    【讨论】:

      猜你喜欢
      • 2013-11-07
      • 1970-01-01
      • 2020-09-11
      • 2021-01-25
      • 2021-01-22
      • 2017-12-23
      • 2020-10-22
      • 2021-01-28
      • 2021-07-08
      相关资源
      最近更新 更多