我有我的填充查询返回一个空数组的问题。我意识到无数的帖子,但是他们没有一个为我提供新的见解。猫鼬填充2文件和2集合返回空阵列
我的第一个模式: station.js
var stationSchema = new Schema({
_id: Number,
Tripcount: [{ type: Number, ref: 'Tripcount'}]
},
{collection: 'stations'}
);
module.exports = mongoose.model('Station', stationSchema);
我的第二个:trips.js
var tripSchema = new Schema({
_id: Number,
Tripcount: Number
},
{collection: 'trips'}
);
module.exports = mongoose.model('Tripcount', tripSchema);
而且我的查询是这样的,根据大多数的答案这应该做的工作:
var Station = require('./station.js');
var query = Station.find()
.populate({
path: 'Tripcount',
model : 'Tripcount'
});
query.exec(function(err, stations){
if(err)
res.send(err);
res.json(stations);
});
使用邮递员获取站,它显示Tripcount: []
。任何想法我做错了什么?
谢谢,但在收藏中没有tripcount数组,可能我没有说清楚。它应该使用来自station collection的_id来匹配tripcount集合中的_id。我想我必须使用findById。 – ffritz
那么你的参考必须是来自Tripcount系列的Station,你必须转置你的查询 – RedJandal
就像Trip.find()。 – ffritz