删除架构上的refs对象

时间:2018-11-07 17:22:39

标签: javascript node.js mongodb mongoose

这是项目架构

var projectSchema = new Schema({
    project_name: String,
    CreatedByID: String,
    followers: [{
        type: Schema.Types.ObjectId, ref: 'User'
    }],
    urls: [{
        type: Schema.Types.ObjectId, ref: 'URLs'
    }]
},
    { // for supporting newer versions of mongodb
        usePushEach: true
    });

const Project = mongoose.model('Project', projectSchema);
module.exports = Project;

这是网址架构

 var projectsModel = require('./project');
    var urlsSchema = new Schema({
        address: { type: String, unique: true, required: true },
        username: String,
        password: String,
    },
        { usePushEach: true });

    urlsSchema.pre('remove', function (next) {
        projectsModel.update(
            { urls: this._id },
            { "$pull": { "urls": this._id } },
            { "multi": true }, next);
    });
    const URLs = mongoose.model('URLs', urlsSchema);
    module.exports = URLs;

我试图编写中间件,以便在删除URL时在Project方案中将其删除,但是中间件不起作用

0 个答案:

没有答案