question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for nested virtuals, virtuals in subdocuments

See original GitHub issue
var 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:closed
  • Created 13 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
matheusdavidsoncommented, Mar 5, 2017

The solution above works, but you’ll also need to set virtuals on for the nested schema:

Variation.set('toJSON', {
    virtuals: true
});
0reactions
cyberwombatcommented, Oct 12, 2014

Figured it out:.

var Variation = new Schema({
  label: {
    type: String
  }
});

// Virtual must be defined before the subschema is assigned to parent schema
Variation.virtual("name").get(function() {

  // Parent is accessible
  var parent = this.parent();
  return parent.title + ' ' + this.label;
});


var Product = new Schema({
  title: {
    type: String
  }

  variations: {
    type: [Variation]
  }
});
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found