How do I populate an imbedded and nested object from another collection?
User Schema:
const walletSchema = new mongoose.Schema(
{
name: {
type: String,
required: true
},
balance: {
type: Number,
required: true
}
}
);
const userSchema = new mongoose.Schema(
{
username: {
type: String,
required: true
},
wallets: {
type: [walletSchema],
default: []
}
}
);
const User = mongoose.model(
'User',
userSchema
);
export default User;
`
Expenses Schema:
const expenseSchema = new mongoose.Schema(
{
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: ‘User’,
required: true
},
walletId: { // ← will store the respective wallet id of the User
type: mongoose.Schema.Types.ObjectId,
ref: ‘User.walets.walletId’ // ← THE PROBLEM
},
amount: {
type: Number,
required: true
}
);
const Expense = mongoose.model(
‘Expense’,
expenseSchema
);
export default Expense;
I imbedded the wallets in the User object as I don't expect the user to have a thousand wallets and I will be accessing the wallets property frequently. Therfore I didn't separate it in it's own collection.
Is there a way to populate this field? or do i have to do it manually? The user's expenses will be shown in a table displaying each expense's information