如何在删除父对象时更新子对象、父对象的引用?

qyuhtwio  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(343)

我之所以提出这个问题,是因为我在研究时很难提出正确的问题。我希望澄清:
我有两个模式:
家长[团队]:

const EquipaSchema = new mongoose.Schema({

    trab1: { 
        type: Schema.Types.ObjectId,
        required: true,
        ref: 'Trab'
    },
    trab2: { 
        type: Schema.Types.ObjectId,
        required: true,
        ref: 'Trab'
    },
    trab3: { 
        type: Schema.Types.ObjectId,
        required: true,
        ref: 'Trab'
    },
    teamName: {
        type: String,
        required: true
    },
    marcsEquipa: [{
        type: Schema.Types.ObjectId,
        ref: 'Marcacao'
    }]
},
{collection: 'Equipas'})

和儿童[项目]:

const MarcacaoSchema = new mongoose.Schema({

    name: { type: String, required: true },
    email: { type: String, required: true },
    date: { type: Date, required: true},
    hour: { type: String, required: true},
    type: { type: String, required: true},
    address: {type: String, required: true},
    state: {type: String, default: 'Pendente'},
    // equipa: { type: Schema.Types.ObjectId, ref: 'Equipa' },
    equipa: {
        type: String,
        default: 'Não'
    },
    cliente: { 
        type: Schema.Types.ObjectId, 
        ref: 'User'
    },
    aval_admin: {
        type: String,
        default: "Sem Avaliação pelo Manager"
    },
    descricao: {
        type: String,
        default: "Sem Descrição"
    },
    aval_client: {
        type: String,
        default: "Sem Avaliação do Cliente"
    },
    avaliado: {
        type: Boolean,
        default: false
    },
    team: {
        type: Schema.Types.ObjectId,
        ref: 'Equipas'
    }

},
{collection: 'Marcacao'}

我的目标是:当一个团队被删除时,团队数组['marcsequipa[]']中的所有项目都会更新为{team:null},这样我就可以为同一个项目分配一个新团队。
我曾经尝试过使用中间件,但它的一些用法现在已经被弃用,并且没有成功。解决这个问题的正确方法是什么?
先谢谢你

mwkjh3gx

mwkjh3gx1#

await Marcacao.updateMany({team: req.params._id}, {team: null});

这就是解决办法。其中req.params.\u id是团队id。

相关问题