2017-02-13 3 views
0

私はそれのRefフィールドを持つスキーマ持っている:私は定義し、それらを登録参照文書をMongoose REST経由で表示しますか?

exports.metricSchema = new Schema({ 
    metricGroup: {type: mongoose.Schema.Types.ObjectId, ref: 'metricGroupSchema', required: true}, 
    metricType: {type: mongoose.Schema.Types.ObjectId, ref: 'metricTypeSchema', required: true}, 
    key: {unique: true, type: String, required: true}, 
    name: {type: String, required: true}, 
    description: String 

}); 

を:

var MetricGroupResource = apprest.resource = restful.model('MetricGroup', schemas.metricGroupSchema) 
    .methods(defaultRestMethods); 
MetricGroupResource.register(apprest, '/rest/metricgroup'); 

、それはこのようマングースのRESTに表示されます:

{ 
"_id": "58a20f5f04ef5789d3ef8fb7", 
"name": "Tangle Index", 
"key": "TI", 
"metricType": "58a20f43f1bbfe89c86bf602", 
"metricGroup": "58a20f43f1bbfe89c86bf600", 
"__v": 0 
} 

mongooseにカスタムの読み込まれたビューを構築せずに参照モデルの詳細を表示する方法はありますか?

+0

あなたは "移入" のようなものを意味ですか? – Zlatko

+0

はい、これに似ていますが、このためのカスタムビューを作成していない – abolotnov

+0

私の頭の上から考えることができる唯一の方法は、実際には余分なドキュメントをモデルに入れ子にすることです。そして、アプリでアップデート戦略を見つけてください。 – Zlatko

答えて

0

あなたが移入機能を使用することができ、あなたがドキュメントに読み込むことができますhere

例:

MyModel.find(query) 
.populate(
    [ 
     { 
      'path': 'metricGroup' 
     }, 
     { 
      'path': 'metricType' 
     } 
    ] 
) 
.exec(function (err, _array) { 
    if (err) { 
    console.log(err); 
    } 
    console.log(_array); //print array with metricGroup and metricType fields with details 
}); 
+0

これにはカスタムビューが必要です。デフォルトのアウトオブボックスモデル登録パターンの一部としてこれを行う方法はありますか?私はこれについて少し具体的に私の質問を更新しました。 – abolotnov

関連する問題