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

Populated paths toJSON options #3833

Closed
paglias opened this issue Feb 2, 2016 · 4 comments
Closed

Populated paths toJSON options #3833

paglias opened this issue Feb 2, 2016 · 4 comments

Comments

@paglias
Copy link
Contributor

paglias commented Feb 2, 2016

I have a model that gets populated from another model's document, the populated model has the option minimize: false set. I'd like to disable it when serializing the populated document:

// Challenge and User has minimize: true
var chal = await Challenge.find(...).populate('user', 'some fields').exec();
chal.toJSON();

I need chal.user to be serialized with minimize: true while having chal serialized with the default minimize option, in this case minimize: false

@vkarpov15
Copy link
Collaborator

Right now we don't support that, either all subdocs use their schema-specified minimize option or the one passed to toJSON(). You can work around it by doing something weird like chal.user.schema.options.minimize = true; but I don't have any really good ideas about how to support this behavior in general.

@paglias
Copy link
Contributor Author

paglias commented Feb 2, 2016

chal.user.schema.options.minimize = true;

is a good idea, I actually figured out that populations are simple find calls so I decided to wrote the logic manually but it easily got very verbose

@vkarpov15
Copy link
Collaborator

Yeah this is a bit of an odd edge case. We don't have any way of defining options at a level between "define at function call" and "define on schema". Perhaps we could make minimize take a function, so you could do something like minimize: function(doc) { if (doc.ownerDocument) { return false; } return true; } so you can say "minimize by default unless this is a child doc". Would that work for your use case?

@paglias
Copy link
Contributor Author

paglias commented Feb 3, 2016

@vkarpov15 in the end I had to stay with my custom logic because if I did something like this:

    User.schema.options.minimize = true;
    let challenges = await Challenge.find(...)
      .populate('user', nameFields)
      .exec();
    User.schema.options.minimize = false;

being Challenge.find async a different request could have called user.toJSON() in the meantime with the wrong minimize option.

But it's really an edge case so don't worry if you don't want to implement it (I probably wouldn't since it's so easy to recreate the populate method manually)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants