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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getters for populate virtuals #9343

Closed
NikoFoX opened this issue Aug 18, 2020 · 3 comments
Closed

Getters for populate virtuals #9343

NikoFoX opened this issue Aug 18, 2020 · 3 comments
Labels
new feature This change adds new functionality, like a new method or class
Milestone

Comments

@NikoFoX
Copy link

NikoFoX commented Aug 18, 2020

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

What is the current behavior?
Current populate virtual doesn't allow you to assign foreign object's field value as a virtual's returned value.

Currently doing:

const gameSchema = Schema({
      title: String,
});

const matchSchema = Schema({
      game: ObjectId,
});

matchSchema.virtual('gameTitle', {
      ref: 'Game',
      localField: 'game',
      foreignField: '_id',
      justOne: true,
      options: {select: {'title': 1}}
});

and returning populated Match gives:

{
      _id: 'some_match_id',
      gameTitle: {
            _id: 'some_game_id',
            title: 'some_game_title'
      }
}

What is the expected behavior?
Same virtual should return Match object with gameTitle as String with current Game title.

{
      _id: 'some_match_id',
      gameTitle: 'some_game_title'
}

I'm also wondering why the select option results in returning both selected field and _id field and not the field alone.

@NikoFoX NikoFoX changed the title Populate virtual doesn't let to return only 1 field of populated object Populate virtual doesn't let to return only selected field's value of populated object Aug 18, 2020
@vkarpov15 vkarpov15 modified the milestones: 5.10.1, 5.10.2 Aug 26, 2020
@vkarpov15
Copy link
Collaborator

The reason why _id is there is that _id is included by default, and Mongoose needs _id to match up the Game to its corresponding Match. Any other properties in gameSchema will be excluded.

The gameTitle: 'some_game_title' output is not something we support. Virtual populate always sets gameTitle to a Mongoose document, we don't currently support transforming the document into a string.

We'll keep this open for a future feature suggestion. I'm thinking we can add support for something like this:

matchSchema.virtual('gameTitle', {
      ref: 'Game',
      localField: 'game',
      foreignField: '_id',
      justOne: true,
      options: {select: {'title': 1}},
      set: doc => doc.title
});

Would that work for you @NikoFoX ?

@vkarpov15 vkarpov15 modified the milestones: 5.10.2, 5.x Unprioritized Aug 27, 2020
@vkarpov15 vkarpov15 added the new feature This change adds new functionality, like a new method or class label Aug 27, 2020
@NikoFoX
Copy link
Author

NikoFoX commented Aug 30, 2020

Shouldn't it be get instead of set? Analogically to schema path getters.
That would work for me very much.

@vkarpov15
Copy link
Collaborator

get sounds better, thanks for the suggestion, we'll go with that.

@vkarpov15 vkarpov15 modified the milestones: 5.x Unprioritized, 5.11 Aug 31, 2020
@vkarpov15 vkarpov15 changed the title Populate virtual doesn't let to return only selected field's value of populated object Getters for populate virtuals Nov 6, 2020
vkarpov15 added a commit that referenced this issue Nov 7, 2020
vkarpov15 added a commit that referenced this issue Nov 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

3 participants