通过mongoose REST显示引用文档的详细信息?
问题描述:
我有在它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
}
有没有方法让猫鼬在不构建自定义填充视图的情况下显示引用模型的详细信息?
答
可以使用填入功能,可以阅读文档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
你的意思是像 “填充”? – Zlatko
是的,与此类似,但没有为这个 – abolotnov
创建自定义视图那么,我能想到的唯一办法就是将这些额外的文档嵌入到您的模型中。然后找出应用程序中的更新策略。 – Zlatko