Ich habe eine Frage zum Entfernen mehrerer Referenzen. Ich habe 3 Schemamodelle -> Ereignis, Post und Kommentar. Event speichert Verweise auf Post-Eins-zu-Viele-Verweise und speichert Referenzen auf Kommentare (Eins-zu-Viele).Mehrere Referenzen entfernen
Ereignisschema
const mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = mongoose.Schema.Types.ObjectId;
const EventSchema = new Schema({
organizer: {
type: ObjectId,
required: true,
},
date: {
start: {
type: Date,
required: true,
},
end: {
type: Date,
required: true,
},
},
name: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
category: {
type: String,
required: true,
},
posts: [{
type: ObjectId,
ref: 'Post',
}],
});
module.exports = mongoose.model('Event', EventSchema);
Beitrag Schema
const mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = mongoose.Schema.Types.ObjectId;
const PostSchema = new Schema({
author: {
type: String,
required: true,
},
date: {
type: Date,
default: Date.now(),
required: true,
},
content: {
type: String,
required: true,
},
comments: [{
type: ObjectId,
ref: 'Comment',
}],
});
module.exports = mongoose.model('Post', PostSchema);
Kommentar Schema
const mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = mongoose.Schema.Types.ObjectId;
const CommentSchema = new Schema({
author: {
name: {
type: String,
required: true,
},
id: {
type: ObjectId,
required: true,
},
},
date: {
type: Date,
default: Date.now(),
},
content: {
type: String,
required: true,
},
});
module.exports = mongoose.model('Comment', CommentSchema);
Schauen Sie sich jetzt folgende Situation an: Ich entferne ein Ereignis, also muss ich auch Post (easy) und ähnliche Kommentare (ups!) Entfernen. Hier ist mein Problem: Wie kann ich das Ereignis leicht mit seinen Referenzen entfernen (das Entfernen eines Ereignisses entfernt automatisch den Beitrag und die zugehörigen Kommentare zu diesem Beitrag)? Ich habe wirklich keine Ahnung. Danke für die Hilfe!