3

I'm trying to create a virtual populator in mongoose, but I'm stumped on this aspect, and I don't know if this is a limitation, a bug, or simply I'm doing something wrong.

The idea is that a user can be member of several groups, and then I can populate a virtual in the group schema by querying users that have its id on the groups array.

I created an example (based on the original mongoose documentation).

Luca Turilli has been into many bands, sometimes at the same time, so the original model (in which band was a single field) doesn't cut for italian power metal.

    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    mongoose.connect('mongodb://localhost/test');
    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function() {
      console.log('connected to db');
    });

    var artistSchema = new Schema ({
        name: String,
        bands: [{type: String, ref: 'band'}]
    });
    
    var bandSchema = new Schema ({
        name: String
    },{
        toJson:{virtuals:true},
        toObject: {virtuals:true}
    });
    bandSchema.virtual('lineup', {
        ref: 'Artist',
        localField: 'name',
        foreignField: 'bands'
    });
    
    var Artist = mongoose.model('Artist', artistSchema);
    var Band = mongoose.model('Band', bandSchema);
    /* Comment this if you already populated the database */
    Band.create({name:'Dreamquest'});
    Band.create({name:'Luca Turilli'});
    Band.create({name:'Rhapsody of Fire'});
    
    Artist.create({name:'Luca Turilli', bands:['Dreamquest','Luca Turilli','Rhapsody of Fire']});
    Artist.create({name:'Olaf Hayer', bands:['Luca Turilli']});
    Artist.create({name:'Sascha Paeth', bands:['Luca Turilli','Dreamquest']});
    Artist.create({name:'Robert Hunecke-Rizzo', bands:['Luca Turilli', 'Dreamquest']});
    Artist.create({name:'Dominique Leurquin', bands:['Dreamquest', 'Rhapsody of Fire']});
    Artist.create({name:'Patrice Guers', bands:['Rhapsody of Fire']});
    Artist.create({name:'Alex Landenburg', bands:['Rhapsody of Fire']});
    /*stop commenting here*/
    
    Band.find(function(err, bands) {
        if (err) return console.error(err);
        console.log(bands);
    });

The expected output would be something along the lines of:

    [ { _id: 5b8fd9eef72e14315b52985f,
        name: 'Dreamquest',
        __v: 0,
        lineup: [Artist entries of Luca,Sascha,Robert and Dominique]},
      .... more bands
      ]

Instead, I get this

        [ { _id: 5b8fd9eef72e14315b52985f,
        name: 'Dreamquest',
        __v: 0,
        lineup: null,
        id: '5b8fd9eef72e14315b52985f' },
      { _id: 5b8fd9eef72e14315b529860,
        name: 'Luca Turilli',
        __v: 0,
        lineup: null,
        id: '5b8fd9eef72e14315b529860' },
      { _id: 5b8fd9eef72e14315b529861,
        name: 'Rhapsody of Fire',
        __v: 0,
        lineup: null,
        id: '5b8fd9eef72e14315b529861' } ]

I've tried to search for examples or people in the same situation, but I haven't found much information. I know I probably could get a getter function to do this, but I want to know if it is possible to do it with virtuals or I'm wasting my time.

Mak
  • 894
  • 2
  • 8
  • 24
Stormbolter
  • 203
  • 2
  • 7

1 Answers1

2

In the end, the response is simple: virtuals:truedoesn't autopopulate the fields, and virtuals don't populate by themselves. It's confusing because it would seem that populated virtual implies that the virtual populates by itself...

Anyway the correct query is:

Band.find({}).populate('lineup').exec(function(err,bands) {
    if (err) return console.error(err);
    console.log(bands);
}

As with any manual population, you can hook into 'find', 'findone' and 'save' (among others) to set it to autopopulate, if you're planning to access this info regularly.

Stormbolter
  • 203
  • 2
  • 7