他のドキュメントを参照したモデルがあります。そのモデルには、参照モデルで使用されるデータを処理できるメソッドが必要です。
'use strict';
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, deepPopulate = require('mongoose-deep-populate');
var MainSchema = new Schema({
childs: [{type:Schema.ObjectId, ref: 'Child'}], //because many-to-one
startDate: {type: Date, default: Date.now},
endDate: {type: Date},
});
MainSchema.methods = {
retrieveChilds: function(callback) {
// deepPopulation of childs not possible??
callback(result);
},
};
MainSchema.plugin(deepPopulate);
module.exports = mongoose.model('Main', MainSchema);
上記のコード例に見られるように、retrieveChilds 関数は現在のスキーマで deepPopulate 関数を実行する必要があります。これは可能ですか、それともモデルの外で起こるべきですか? (これにより、コードが重複する場合があります)