2016-03-08 40 views
2

這是我的架構如何刪除模型對於一個項目的id用貓鼬和的NodeJS

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存儲 項目。

回答

2

可以使用$pull更新操作員從一個數組中刪除的項目如下:

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