我有一个包含ref字段的架构:
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');
它将在mongoose REST中显示为:
{
"_id": "58a20f5f04ef5789d3ef8fb7",
"name": "Tangle Index",
"key": "TI",
"metricType": "58a20f43f1bbfe89c86bf602",
"metricGroup": "58a20f43f1bbfe89c86bf600",
"__v": 0
}
有没有办法让mongoose显示引用模型的详细信息而不构建自定义填充视图?
答案 0 :(得分:0)
您可以使用populate函数,您可以阅读文档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
});