Automattic / Automattic/mongoose

virtual populate that relies on another virtual doesn't return populated values

Open
#6,678 5 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

```

userSchema ({
profile: { type: String, default: ''}
})

companySchema ({
name: {type: String}
})

companyUserSchema ({
_company: {type: Schema.Types.ObjectId, ref: 'Company'},
_user: {type: Schema.Types.ObjectId, ref: 'User'},
_team_members: [{type: Schema.Types.ObjectId, ref: 'User'}]
})

userSchema.virtual('_company_user', {
ref: 'CompanyUser',
localField: '_id',
foreignField: '_user'
})

userSchema.virtual('team_members', {
ref: 'User',
localField: '_company_user._team_members'
foreignField: '_id'
})

```

user.team_members returns an array of ObjectIds unpopulated.

```

user
{
profile: 'profile1',
_company_user: {
_company:
_user:
_team_members: []
},
team_members: [] //i was expecting this to be populated
}

```

Contributor guide

Open the contributing guide

Research direction

Start with the userSchema.virtual definitions and reproduce the nested virtual-populate case shown in the issue. Trace how team_members is resolved through _company_user and compare the returned values with the expected populated User documents; done means team_members is populated rather than containing ObjectIds.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.