在我的MEAN应用程序(Angular2)中,当删除对象本身时,我希望删除所有引用的对象。我使用Mongoose和remove中间件。因此,我的question.js文件如下所示:
我知道可以使用find方法来查找多个元素并添加查询。但我只发现了查找一个特定id或仅从数组中删除它们的内容。但我希望对象被移除,而不仅仅是在那个数组中的引用被删除。
如果有重复,请随意关闭此问题,但我在谷歌搜索、堆栈溢出和相关主题中都没有找到答案。
谢谢您的帮助!
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Answer = require('../models/answer');
var QuestionSchema = new Schema({
content: {type: String, required: true},
questionTxt: {type: String, required: true},
position: {type: Number, min: 0, required: true},
answers: [{type: Schema.Types.ObjectId, ref: "Answer"}],
followUpQuestions: [{type: Schema.Types.ObjectId, ref: "Question"}],
additionalInfoText: {type: String},
lastChangedBy: {type: Schema.Types.ObjectId, ref: 'User'},
lastChanged: {type: Date},
isRoot: {type: Boolean}
});
/**********************************************
* Deletes all answers and questions referenced by this question
***********************************************/
schema.post('remove', function(doc) {
var deletedQuestion = doc;
//code missing to find the answers and delete all referenced answers
});
});
module.exports = mongoose.model('Question', QuestionSchema);
我知道可以使用以下方法来查找:
Answer.findById(doc.answer, function(err, doc){});
我知道可以使用find方法来查找多个元素并添加查询。但我只发现了查找一个特定id或仅从数组中删除它们的内容。但我希望对象被移除,而不仅仅是在那个数组中的引用被删除。
如果有重复,请随意关闭此问题,但我在谷歌搜索、堆栈溢出和相关主题中都没有找到答案。
谢谢您的帮助!