Automattic / Automattic/mongoose

Allow virtual populate options to be a function

Open
#8,579 1 comment 0 reactions 0 assignees View on GitHub
new feature
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

**Do you want to request a *feature* or report a *bug*?**
Feature

**What is the current behavior?**
Currently, the options for virtual populate are mostly static (with the exception of the `match` field.

**What is the expected behavior?**
It would be great if I could specify `options` to be a function that returns an object of options. [`mongoose-auto-populate` supports this](https://plugins.mongoosejs.io/plugins/autopopulate#it-can-specify-a-function-that-returns-options).

My use case: I have [a plugin](https://www.npmjs.com/package/mongoose-populate-options) that adds certain options to a query. It also looks at all the existing populate queries and adds the options there as well. But if virtual populate field does two levels of population, the extension can't add the populate option to the second query. Allowing a function to be used for options will allow me to look at the options in the top query and copy the ones I want to be used for the second query.

**What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.**
5.8.11

Contributor guide

Open the contributing guide

Research direction

Start at Mongoose's virtual populate handling and compare the requested function form for options with the existing function support for match. Trace how options move through nested populations. Done means options can be supplied as a function returning an options object and the described two-level virtual populate case receives the intended values, with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb, node.js
Domain
backend, database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.