使用 Mongoose 填充嵌套数组
Populate nested array with Mongoose
我有一个如下所示的用户架构,我正在尝试填充实时项目数组,但我不知道如何访问它。
const userSchema = new Schema({
local: {
email: String,
username: String,
password: String,
liveProjects: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'liveProject'
}]
},
google: {
googleId: String,
email: String,
username: String,
liveProjects: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'liveProject'
}]
}
});
const User = mongoose.model('user', userSchema);
module.exports = User;
如果没有嵌入我可以直接使用
User.findById(id).populate('liveProjects').exec((err,projects)=>{});
但是我如何才能访问 'local.liveProjects'
或 'google.liveProjects'
以便填充它们?
此处的用户架构
`let UserSchema = new Schema({
email: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
profile: {
firstName: {type: String,required: true},
lastName: {type: String,required: true},
address1: {type: String},
address2: {type: String},
city: {type: String},
state: {type: String},
zip: {type: String},
phone: {type: String,required: true},
avatar:{ type: Schema.Types.ObjectId, ref: 'Avatar'},
shippingAddress:{
address1: {type: String},
address2: {type: String},
city: {type: String},
state: {type: String},
zip: {type: String}
},
},
redemptionCards : [{ type: Schema.Types.ObjectId, ref: 'CardCodes' }]
});`
获得兑换卡的逻辑:-
User.findOne({ email }).populate("redemptionCards")
.exec(function (err, user) {
CardData.populate(user.redemptionCards, {path: 'redemptionCards',match: { _id: { $ne: null }}}, function (err, cards) {
console.log(cards);
});
仅供参考 - CardData 是硬编码的 JSON 文件。希望这有帮助。
事实证明它就像
一样简单
User.findById(id).populate('local.liveProjects').exec((err,projects)=>{});
我有一个如下所示的用户架构,我正在尝试填充实时项目数组,但我不知道如何访问它。
const userSchema = new Schema({
local: {
email: String,
username: String,
password: String,
liveProjects: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'liveProject'
}]
},
google: {
googleId: String,
email: String,
username: String,
liveProjects: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'liveProject'
}]
}
});
const User = mongoose.model('user', userSchema);
module.exports = User;
如果没有嵌入我可以直接使用
User.findById(id).populate('liveProjects').exec((err,projects)=>{});
但是我如何才能访问 'local.liveProjects'
或 'google.liveProjects'
以便填充它们?
此处的用户架构
`let UserSchema = new Schema({
email: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
profile: {
firstName: {type: String,required: true},
lastName: {type: String,required: true},
address1: {type: String},
address2: {type: String},
city: {type: String},
state: {type: String},
zip: {type: String},
phone: {type: String,required: true},
avatar:{ type: Schema.Types.ObjectId, ref: 'Avatar'},
shippingAddress:{
address1: {type: String},
address2: {type: String},
city: {type: String},
state: {type: String},
zip: {type: String}
},
},
redemptionCards : [{ type: Schema.Types.ObjectId, ref: 'CardCodes' }]
});`
获得兑换卡的逻辑:-
User.findOne({ email }).populate("redemptionCards")
.exec(function (err, user) {
CardData.populate(user.redemptionCards, {path: 'redemptionCards',match: { _id: { $ne: null }}}, function (err, cards) {
console.log(cards);
});
仅供参考 - CardData 是硬编码的 JSON 文件。希望这有帮助。
事实证明它就像
一样简单User.findById(id).populate('local.liveProjects').exec((err,projects)=>{});