Support for nested virtuals, virtuals in subdocuments
See original GitHub issuevar Page = new Schema({
author: {
first_name: String
, last_name: String
}
});
Page.virtual("author.full_name").get(function() {
return this.author.first_name + " " + this.author.last_name;
});
// Later
myPage = new Page({author: {first_name: "John", last_name: "Doe"}});
myPage.author.full_name; // == undefined
myPage.get("author.full_name"); // == "John Doe"
I haven’t tried making a new schema for the author with its own virtuals. Maybe that would work, but according to other issue reports, sub-schemas work optimally in arrays a.t.m., not as plain subdocuments.
Looking at the source, this doesn’t look to be impossible, just missing some clever split(“.”) logic while adding the virtual to the schema tree.
The get method works fine for the time being, but the other syntax would be preferrable. 😃
Issue Analytics
- State:
- Created 13 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Implement Typegoose Virtuals on Nested Subdocuments
We are trying to implement the following Virtual fields: choice.score that shall equal the choice offset + votes.length; poll.totalScore that ...
Read more >Mongoose Virtuals
In Mongoose, a virtual is a property that is not stored in MongoDB. Virtuals are typically used for computed properties on documents.
Read more >How to use virtual on a nested field in mongoose-node.js
Below is one way to implement the domain property using a virtual. You define virtuals on a schema using the Schema#virtual() function.
Read more >Understand Sub-documents & Nested documents in Mongoose
Subdocuments are documents embedded in other documents. In Mongoose, this means you can nest schemas in other schemas. However; nested paths ...
Read more >The deep virtual population in mongoose is actually very simple!
mongoose's virtual population is very useful tool. You can mention population path in schema as a virtual field and mongoose will automatically query...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The solution above works, but you’ll also need to set virtuals on for the nested schema:
Figured it out:.