I Have this user model Which has blog model as reference and blog has comments collections as reference
const userSchema = new Schema(
{
email: {
type: String,
required: true,
index: {
unique: true
}
},
password: {
type: String,
required: true
},
name: {
type: String,
required: true
},
website: {
type: String
},
bio: {
type: String
}
},
{
timestamps: {
createdAt: "created_at",
updatedAt: "updated_at"
}
}
);
userSchema.virtual("blogs", {
ref: "blogs",
localField: "_id",
foreignField: "author"
});
i want to delete blog data as well and any nested collection within blog collection too how can i delete it?
if I got you right, this will help.
function deleteUserById(id){
UserModel.findById(id, async (err, user) => {
try{
await deleteBlodById(user.blog._id);
UserModel.findByIdAndRemove(id)
}catch(e){
throw e;
}
})
}
function deleteBlogById(id){
BlogModel.findById(id, async (err, blog) => {
// ... The pretty same thing like before, just delete
})
}
I hope that will help. If your question was like Is there any function that automatically remove document and all its referenced documents
I cannot find anything like that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.