Ich möchte alle Unterdokumente meiner Sammlung löschen.Löschen von Filialdokumenten mit Mongoose-Rückgabefehler?
Mungo Schema:
//productSchema
var pdtSchema = new Schema({
"productId" : {type : String},
"product" : {type : String},
"item no" : {type : String},
});
var shopSchema = new Schema({
"providerId" : {type : String},
"provider" : {type : String},
"products" : [pdtSchema]
},{collection:"shopdetails"});
module.exports.Shops = mongoose.model('Shops',shopSchema);
module.exports.Products = mongoose.model('Products',pdtSchema);
Ich habe einen Großteil der Daten in der Sammlung gespeichert und ich brauche alle Produkte zu löschen (dh die ganze pdtSchema
Daten).
Code:
router.post('/delete',function (req,res) {
var providerId = req.body.providerId;
model.Shops.findById({"providerId" : providerId},function(err, doc) {
console.log(doc.products) // returns whole products here...
doc.products.remove();
doc.save(function(err,data){
res.json({"msg":"deleted"});
});
});
});
Fehler:
(node:16351) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ValidationError: CastError: Cast to ObjectID failed for value "[Function]" at path "_id"
es ein Fehler ist zu geben, weil Sie model.findById() Funktion verwenden, die mit _id funktioniert, aber Sie versuchen, es mit ProviderID die uns vom Typ String –