My simplified schema looks like this:
var personSchema = new Schema({
firstName: String,
lastName: String
});
var teamSchema = new Schema({
people: [personSchema]
});
module.exports = mongoose.model('Team', teamSchema);
And I want to find all the persons named "Alan". I looked at this very similar question, but it finds teams, not people. Same here, I think. Is there a query that returns persons even if I don't have a persons collection?
I think I can use the cited techniques to find teams, then pull out their people. Something like this, I guess:
teams
.find({})
.populate({
path: 'people',
match: { firstName: { $eq: "Alan" }}
})
.exec().then(function(teams) {
var people = [];
// walk through the found teams, looking through all the people
// whenever one is found named Alan, add it to the people array
});
But there's a more direct way, right?