如何在猫鼬中填充具有对象数组的对象? [英] How to populate object having array of object in mongoose?
本文介绍了如何在猫鼬中填充具有对象数组的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将 adminId
路径填充到 User
模型.
这是代码
I want to populate the adminId
path to User
Model.
Here is the code
adminInfo: {
_id: false,
adminId: [{
type: Schema.Types.ObjectId,
ref: 'User'
}]
}
这是用户架构的一部分:
Here is a part of user schema:
// user schema
const UserSchema = new mongoose.Schema({
name: {
firstName: {
type: String,
trim: true,
required: true,
},
lastName: {
type: String,
trim: true
}
},
email: {
type: String,
trim: true,
required: true,
unique: true,
lowercase: true
},
phone: {
type: String,
trim: true,
minlength: 10,
},
password: {
type: String,
trim: true,
required: true,
minlength: 6
}
});
我尝试使用 .populate('adminInfo.adminId')
但它给出了空数组 [] 而 .populate('adminInfo')
给出了一组管理员 ID但没有填充到用户模型
I have tried using .populate('adminInfo.adminId')
but it's giving empty array [] whereas .populate('adminInfo')
giving array of admins ids but not getting populated to User model
推荐答案
我认为 .populate('adminInfo.adminId')
方法没有任何问题.
i don't think there is any problem with .populate('adminInfo.adminId')
method.
您确定 ref 字段在 CamelCase 中.
are you sure that ref field is in CamelCase .
如果不是,请尝试更改引用字段 ->
If not, try to change ref field ->
adminInfo: {
_id: false,
adminId: [{
type: Schema.Types.ObjectId,
ref: 'user'
}]
}
这篇关于如何在猫鼬中填充具有对象数组的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文