Saturday 3 June 2017

Remove referencing documents in MongoDB [duplicate]

This question already has an answer here:

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