2017-12-01 4 views
1


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!

Antwort

0

0) Lesen Sie das Ereignis, das Sie

cont eventObj = Event.findOne({ 
    _id: myEventId, 
}) 

1) Erhalten Sie alle Kommentare zu dem Ereignis löschen möchten

const posts = eventObj.posts; 

const postObjs = Post.find({ 
    _id: { 
    $in: posts, 
    }, 
}); 

2) Holen Sie sich alle Kommentare zu den Beiträgen, die Sie wollen mit Bezug zu löschen zu löschen

const comments = postsObjs.reduce((tmp, x) => [ 
     ...tmp, 
     ...x.comments, 
    ], []); 

3) löschen alle Kommentare

Comments.remove({ 
    _id: { 
     $in: comments, 
    }, 
    }); 

4) Löschen aller Beiträge

Post.remove({ 
    _id: { 
     $in: posts, 
    }, 
    }); 

5) Löschen des Ereignis

Event.remove({ 
    _id: { 
     $in: event, 
    }, 
    }); 
Verwandte Themen