有两个模式用户和问题。用户可以问问题,而Qusetions模式具有存储用户ID和名称的用户字段。现在,我要填充用户提出的所有问题。
我会尝试使用猫鼬进行虚拟填充,但是它不起作用。帮助我解决这个问题。 谢谢
用户架构
const mongoose = require('mongoose');
const passportLocalMongoose = require('passport-local-mongoose');
const Question = require('../models/question');
const userSchema = mongoose.Schema({
name: {
type: String
},
email: {
type: String
},
password: {
type: String
}
})
userSchema.plugin(passportLocalMongoose);
userSchema.virtual('ques', {
ref: 'Question',
localField: '_id',
foreignField: 'user.id'
})
const User = mongoose.model('User', userSchema);
module.exports = User;
问题模式
const mongoose = require('mongoose');
const questionSchema = mongoose.Schema({
title: {
type: String
},
description: {
type: String
},
answers: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Answer'
}],
user: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
username: String
}
}, {
timestamps: true
})
const Question = mongoose.model('Question', questionSchema);
module.exports = Question;