猫鼬不返回对象数组中的所有字段

时间:2020-10-01 05:20:13

标签: mongoose

我正在尝试从数据库中获取章节列表。但是,并不是我的视频模型的Chapters数组中的所有字段都被返回。当我查询数据库并打印video.chapters start_timeend_time的内容时,不会返回。我已经使用Compass确认,我要查询的视频的章节确实包含start_timeend_time

// video.model.js
const chapterSchema = new mongoose.Schema({
    start_time: { type: Number, required: true },
    end_time: { type: Number, required: true },
    title: { type: String, default: null },
}, { _id: false })

const videoSchema = new mongoose.Schema({
    uploader: { type: ObjectId, ref: 'Uploader', default: null },
    duration: { type: Number, default: null },
    chapters: [{ type: chapterSchema }],
});

export default mongoose.model('Video', videoSchema);
// route.js
const video = (await Video.findOne({
    extractor: req.params.extractor,
    id: req.params.id
}, '-_id uploader duration chapters')
     .populate('uploader')
     .exec()
)?.toJSON();

console.dir(video.chapters);

/*
{ title: 'Introduction' },
{ title: 'Acknowledgements' },
{ title: 'Chapter 1 - Chapter Title' },
...
Each of these objects should also have start_time and end_time properties
*/

如何让猫鼬返回数组章节对象中的所有字段?

0 个答案:

没有答案