Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request - Deep Populate Support in Mongoose Virtuals #14227

Open
2 tasks done
siddiquiaffan opened this issue Jan 2, 2024 · 0 comments
Open
2 tasks done

Feature Request - Deep Populate Support in Mongoose Virtuals #14227

siddiquiaffan opened this issue Jan 2, 2024 · 0 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class

Comments

@siddiquiaffan
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

馃殌 Feature Proposal

Issue Description:

I would like to request the addition of a feature that supports deep populate in Mongoose virtuals. Currently, the populate method allows for deep population of related data from other collections/models, but this functionality is not available when using virtuals.

Use Case:

In scenarios where virtuals are used to define relationships between models, the lack of deep populate support poses a limitation. For example, consider a case where a User model has a virtual field customerId, and there are other models such as Subscription and Product. To retrieve related data deeply nested, it is currently not possible with virtuals.

Example with populate method:

User.findOne({ name: 'John' }).populate({
  path: 'customerId',
  model: 'Subscription',
  populate: {
    path: 'productId',
    model: 'Product',
    foreignField: 'id'
  }
})

Requested Feature:

I propose adding deep populate support to virtuals in Mongoose. This would enhance the flexibility and utility of virtuals, enabling users to retrieve related data from multiple levels of nesting.

Suggested Implementation:

The implementation could follow a syntax similar to the populate method, allowing users to specify the path, model, and other relevant options for each level of deep population within a virtual.

Additional Information:

Mongoose Version: [Specify the version you are using]
Node.js Version: [Specify the version you are using]
Thank you for considering this feature request. I believe that adding deep populate support to virtuals will further empower developers to create expressive and efficient MongoDB queries with Mongoose.

Motivation

No response

Example

const UserSchema = new Schema({
    id: String,
    name: String,
    customerId: String,
})

UserSchema.virtual('subscriptions', {
    ref: 'Subscription',
    localField: 'customerId',
    foreignField: 'customerId',
    justOne: false,

    // new
    populate: { // or virtual
        ref: 'Product',
        localField: 'productId',
        foreignField: 'id',
        // path / as
        path: 'product',
    }
})

const User = mongoose.model('User', UserSchema)

const SubscriptionSchema = new Schema({
    id: String,
    customerId: String,
    productId: String,
})
const Subscription = mongoose.model('Subscription', SubscriptionSchema)

const ProductSchema = new Schema({
    id: String,
    name: String,
})

const Product = mongoose.model('Product', ProductSchema)

const data = await User.findOne({ name: 'Affan' }).populate('subscriptions')

/*
 => result   
 {
     id: 1,
     name: 'John',
     customerId: 'cus_123',
     subscriptions: [
         id: 'sub_123',
         customerId: 'cus_123',
         productId: 'prod_123',
         product: {
             id: 'prod_123',
             name: 'Product 1',
         }
     ]
 }
 */
@siddiquiaffan siddiquiaffan added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class labels Jan 2, 2024
@vkarpov15 vkarpov15 added this to the 8.1 milestone Jan 3, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.1, 7.x Unprioritized Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

2 participants