This question already has an answer here:
- Cascade style delete in Mongoose 1 answer
I am having two collection User and Referral.Referral is related to user [Referenced relation].Suppose i am deleting a document from User collection,i need to delete the corresponding document in Referral collection also.How can i achive this?
User Schema
var mongoose = require('mongoose');
var userSchema = new mongoose.Schema({
firstname: {
type: String
},
lastname: {
type: String
},
email: {
type: String,
unique: true
}
});
var UserModel = mongoose.model('User', userSchema);
module.exports = {
user: UserModel
}
Referral Schema
var mongoose = require('mongoose');
var refferalSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
points: {
type: Number
}
});
var ReferralModel = mongoose.model('Referral', refferalSchema);
module.exports = {
referral: ReferralModel
}
via Muhsin
No comments:
Post a Comment