-
Notifications
You must be signed in to change notification settings - Fork 36
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
How can I specify that the auto-populated document should not include or calculate its virtual properties? #121
Comments
The key detail is that virtuals aren't included until they are accessed. In this case, exampleVirtual would only execute when you explicitly use
Do either of those help? |
Yes, if I were to check if
Trying I wanted to add this behavior directly to the I forgot to include in my example const clanSchema = new mongoose.Schema(
{
name: {
type: String,
required: [true, "Please add a name value"],
unique: true,
},
members: [memberSchema],
},
{
timestamps: true,
toJSON: { virtuals: false },
toObject: { virtuals: false },
}
); Anyhow, my ideal solution would be to directly specify in the
I would love to just be able to specify a virtuals option in the autopopulate settings like: user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
autopopulate: {
select: "name avatar avatarFrame",
maxDepth: 1,
virtuals: false,
},
}, |
How about autopulating user with The issue is that virtuals are computed properties defined on the Another potential option is that Mongoose could support setting default |
How can I specify that the auto-populated document should not include or calculate its virtual properties?
For example, I have a clan member schema that includes the
User
document. I only want to auto-populate the User'sname, avatar, avatarFrame
and nothing more.I also had the issue that since the
User
itself has autopopulate fields (inventory
for example), those were being included despite not meant to be. I fixed that by settingmaxDepth: 1
, not sure if that's the intended fix.Though that now means since the virtual fields are still trying to be included, it causes an error since it's only intended to be used when the
User
is fully autopopulated (not needed in this case.)Member schema:
User schema:
The text was updated successfully, but these errors were encountered: