1
我有2个不同的集合定义的2级架构,我需要填充其中的一个到另一个:填充来自两个集合
stationmodel.js
var stationSchema = new Schema({
StationName: 'string',
_id: 'number',
Tripcount: [{ type: Schema.Types.ObjectId, ref: 'Tripcount'}]
},
{collection: 'stations'}
);
module.exports = mongoose.model('Station', stationSchema);
tripmodel.js
var tripSchema = new Schema({
_id: { type: Number, ref: 'Station'},
Tripcount: 'number'
},
{collection: 'trips'}
);
module.exports = mongoose.model('Tripcount', tripSchema);
根据猫鼬填充documentation,这是要走的路。当我使用Postman获取电台时,我遇到了“Tripcount”仍然为[]
的问题。
我的数据库结构的 '站' 集合:
{
"_id": 1,
"StationName": "Station A",
}
而对于 '旅行' 收藏:
{
"_id": 1,
"Tripcount": 6
}
我routes.js:
module.exports = function(app) {
app.get('/stations', function(req,res) {
var query = Station.find().populate('Tripcount');
query.exec(function(err, stations){
if(err)
res.send(err);
res.json(stations);
});
});
};
我可以似乎没有发现错误,也许有人在这里可以发现我犯的一个错误。
谢谢。我根据你的建议改变了一切,然而邮差仍然显示'Tripcount:[]'的一切。我把我改变的routes.js放在我的问题中。 – ffritz
我认为这涉及到“猫鼬填充空阵列”问题的人也有。 – ffritz
我已经添加了一个替代方案,您可以尝试,前提是“工作台”收集与'_id'键的'trips'收集有关。 – chridam