I am trying to delete a MongoDB document but it's not getting deleted
My schema is
const mongoose = require("mongoose");
const InvestorSchema = mongoose.Schema({
name: {
type: String,
index: true,
required: true
},
logoUrl: {
type: String,
required: true
},
website: {
type: String,
index: true,
unique: true,
required: true
}
});
module.exports = mongoose.model("Investor", InvestorSchema);
and I tried using these but none of them removed the document, Also i'm running on localhost with no users and roles.
// Required models
const InvestorModel = require("mongoose").model("Investor");
const deletedInvestor = InvestorModel.remove({ _id });
const deletedInvestor = InvestorModel.deleteOne({ _id });
const deletedInvestor = InvestorModel.findByIdAndRemove(_id);
const deletedInvestor = InvestorModel.findOneAndRemove({_id});
const deletedInvestor = InvestorModel.findByIdAndDelete(_id);
const deletedInvestor = InvestorModel.findOneAndDelete({_id});
How do i solve this?
deletedInvestor = await InvestorModel.deleteOne({ _id });.awaitis necessary for any kind of I/O operation that is essentially an "async" operation. For NodeJS just presume ALL I/O is "async". Or of course, read the documentation, because it will tell you. Withoutawait, nothing actually happens. Or rather your program probably exits before it has any opportunity to happen.mongoose.connect()of course. And which also is "async I/O" and yo must await.