房间架构
var dayData = require('../day-schema');
var roomData = new Schema
(
{
room_number : { type: Number },
day_of_the_week : [{
type: Schema.Types.ObjectId,
ref: 'dayData'
}]
},
{
collection: 'roomData'
}
);
module.exports = mongoose.model('roomData', roomData);
日架构
var roomData = require('../room-schema');
var dayData = new Schema
(
{ room_number : { type: Schema.Types.ObjectId,
ref: 'roomData'
},
day_of_the_week : { type: String },
room_schedule : [{ class_start: String,
class_end: String,
class_code: String,
}]
},
{
collection: 'dayData'
});
module.exports = mongoose.model('dayData', dayData);
这是我为每个房间调用数据的代码
var roomDataId = req.params.roomDataId;
RoomData.findById(roomDataId, 'day_of_the_week')
.populate('day_of_the_week')
.exec(function(err, RoomData) {
if(err) {
res.end(err);
}
else {
res.render('roomData', { RoomData: RoomData});
}
});
我得到的输出是这样的: '{_id:58ce60e194ae2b0718544c8f,DAY_OF_THE_WEEK: '星期二',__v:0,room_schedule:[]},{_id:58ce60ab94ae2b0718544c8e,DAY_OF_THE_WEEK: '星期一',__v:0,room_schedule:[]}'
我想要的输出是这样的:
我在尝试单独获取数组的值时遇到问题。当我尝试在else中执行另一个findById时,我根本得不到任何输出。任何帮助将非常感激。谢谢x
答案 0 :(得分:0)
populate()
可以带2个参数。第一个是引用的集合,第二个是该集合的字段。
var roomDataId = req.params.roomDataId;
RoomData.findById(roomDataId, 'day_of_the_week')
.populate('day_of_the_week', 'day_of_the_week')
.exec(function(err, RoomData) {
.
.