【发布时间】:2023-04-09 11:08:01
【问题描述】:
我的填充查询返回一个空数组的问题。我知道无数的帖子,但没有一个为我提供新的见解。
我的第一个架构: 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);
});
使用 Postman 获取电台,它显示 Tripcount: []。任何想法我做错了什么?
【问题讨论】:
标签: node.js mongodb mongoose mongoose-populate