0

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?

Community
  • 1
  • 1
user1272965
  • 2,814
  • 8
  • 29
  • 49
  • If you want to use `populate`, your schema should be `people: [{type: ObjectId, ref: 'Person'}]` – zangw Mar 04 '16 at 01:13
  • 1
    For this schema `people: [personSchema]` is subdocument. not population – zangw Mar 04 '16 at 01:14
  • 1
    @zangw Good eyes. Therefore the "dot notation" query would apply to get the desired result. – Blakes Seven Mar 04 '16 at 01:16
  • `Is there a query that returns persons even if I don't have a persons collection`, I am confused with it, could you please give us more details? – zangw Mar 04 '16 at 01:21
  • @zangw, in another file, I can access the team collection, calling its statics and instance methods. I'd like to create something on team.statics that returns all of the people (on all of the teams) that match a person criteria. – user1272965 Mar 04 '16 at 01:36
  • Possible Duplicate of [Retrieve Only the Queried Element in an Object Array in MongoDB Collection](http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – Blakes Seven Mar 04 '16 at 01:44

1 Answers1

1

Per your comments, maybe you can try this one

teamSchema.statics.findPeople = function(name, cb) {
    return this.find({'people.firstName': name}, {'people.$': 1}, cb);
}

var Team = mongoose.model('Team', teamSchema);

Team.findPeople('Alan', function(err, data) {
     if (err)
         console.log(err);
     else
         console.log(data);
})
zangw
  • 43,869
  • 19
  • 177
  • 214
  • Thanks very much. Can you explain what `{'people.$': 1}` means? – user1272965 Mar 04 '16 at 02:12
  • Oh and one other nit: is it much different if I have an array of first names? Something like $in ? – user1272965 Mar 04 '16 at 02:14
  • @user1272965, for `$`, The positional $ operator limits the contents of an from the query results to contain only the first element matching the query document. here is the doc https://docs.mongodb.org/manual/reference/operator/projection/positional/#proj._S_ – zangw Mar 04 '16 at 02:17
  • @user1272965, $in is used to query in one array, if there is only one element in the query array, I think it could be same as without $in. – zangw Mar 04 '16 at 02:22
  • I'm still a little baffled, but I sure appreciate that you took the time to give a patient and careful answer. Also, that pointer into the doc is great. Hadn't been able to find the reference section and I'm glad to have finally found a good place to study. – user1272965 Mar 04 '16 at 14:56