Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findOne Subdocument in Mongoose

People also ask

What is a subdocument in mongoose?

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.

What is findOne in mongoose?

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.

What are subdocuments in MongoDB?

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.

What is a sub document?

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);
        }
    }
);