In Mongoose, subdocuments are documents that are nested in other documents. You can spot a subdocument when a schema is nested in another schema. Note: MongoDB calls subdocuments embedded documents.
Mongoose | findOne() Function The findOne() function is used to find one document according to the condition. If multiple documents match the condition, then it returns the first document satisfying the condition.
Subdocuments are documents embedded in other documents. In Mongoose, this means you can nest schemas in other schemas. Mongoose has two distinct notions of subdocuments: arrays of subdocuments and single nested subdocuments.
subdocument (plural subdocuments) (computing, wordprocessing) A document making up part of a larger document.
You're missing the teamMembers
level of your object, so your code needs to change to something like this:
Team.findOne({'teamMembers.username': 'Bioshox'}, {'teamMembers.$': 1},
function (err, team) {
if (team) {
console.log(team.teamMembers[0].email);
}
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With