Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Populate takes the property name of the property you are trying to retrieve. This means that you should use 'hometeam' instead of 'hometeam.name'. However, you want to retrieve the name of the team so you could filter for that. The call would then become..</p> <pre><code>Match.findOne({}).populate('hometeam', {name: 1}).exec(function(err, teams) </code></pre> <p>Now you have a property called 'hometeam' with in that the name. Have fun :)</p> <p><strong>EDIT</strong></p> <p>Showing how to have a single mongoose instance in more files to have correct registration of schemas.</p> <p><strong>app.js</strong></p> <pre><code>var mongoose = require('mongoose'); var Team = require('./schemas/team-schema')(mongoose); var Match = require('./schemas/match-schema')(mongoose); // You can only require them like this ONCE, afterwards FETCH them. var Team = mongoose.model('Team'); // LIKE THIS </code></pre> <p><strong>schemas/match-schema.js</strong></p> <pre><code>module.exports = function(mongoose) { var Match = new mongoose.Schema({ 'key' : { unique: true, type: Number, default: getId }, 'hometeam' : { type: mongoose.Schema.ObjectId, ref: 'Team' }, 'awayteam' : { type: mongoose.Schema.ObjectId, ref: 'Team' } }); return mongoose.model('Match', Match); }; </code></pre> <p><strong>schemas/team-schema.js</strong></p> <pre><code>module.exports = function(mongoose) { var Team = new mongoose.Schema({ 'key' : { unique : true, type : Number, default: getId }, 'name' : { type : String, validate : [validatePresenceOf, 'Team name is required'], index : { unique : true } } }); return mongoose.model('Team', Team); }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload