集合是主集合
var FeedsSchema = new mongoose.Schema({
categoryName: {
type: String,
unique: true,
required: true,
ref: 'feeds',
trim: true
},
categoryLogo: {
type: String,
unique: false,
required: true,
trim: true
},
status:{
type: String,
required: true,
},
date:{
type: String,
required:true
}
});
var categories = mongoose.model('categories', FeedsSchema);
module.exports = categories;
集合是儿童集合
var UserSchema = new mongoose.Schema({
user_id: {
type: String,
ref: 'user',
required: true,
trim: true
},
preferences :{
type: String,
required: true,
ref: 'categories',
trim: true
},
status :{
type: String,
required: true,
trim: true
}
});
var Preferences = mongoose.model('preferences', UserSchema);
module.exports = Preferences;
需要显示主集合中的所有值&需要在主要区域中推送相关字段以显示它在第二个集合中 请建议实施该方法的相关方法
答案 0 :(得分:0)
声明
Schema = mongoose.Schema;
将首选项的类型设置为Schema.ObjectId而不是String。
现在,当您选择用户列表或用户byId或..填充首选项属性时
例如:User.find().populate('preferences') .....
你可以找到mongoose populate