MEANアプリケーション(Angular2)では、オブジェクト自体を削除するときに、参照されているすべてのオブジェクトを削除します。私はMongooseをミドルウェアを削除して使用しています。だから私のquestion.jsファイルは次のようになります。Mongoose:参照オブジェクトを削除するときに配列内の参照オブジェクトをすべて削除する
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メソッドを使用することができることとクエリを追加します。しかし、私はちょうど1つの特定のIDを見つけるか、配列からそれらを削除するためのものを見つけました。しかし、私はオブジェクトがその配列内の参照だけでなく削除されることを望みます。
重複している場合は、この質問を終了しても構いませんが、グーグル、スタックオーバーフロー、関連トピックの後に答えが見つかりませんでした。あなたの助けのための
ありがとう!
可能な重複[MongoDBの/マングースfindMany - 配列にリストされたIDを持つすべてのドキュメントを検索](http://stackoverflow.com/questions/8303900/mongodb-mongoose-findmany-find-all-documents-with- ids-listed-in-array)である。これは確かに重複しています。上記は、あなたが行く必要がある場所にあなたを取得する必要があります。 –
@Brudus:ミドルウェアの使用に関するあらゆる更新。それはあなたのために働いたのですか? –