Reputation: 4296
My database structure is looking like this :
{
'name' : 'entry one'
'project' :
[
{companyName : 'a name', contactPerson : [{ work_email: '[email protected]'}] } ,
{companyName : 'a name1', contactPerson : [{ work_email: '[email protected]'}] } ,
{companyName : 'a name2', contactPerson : [{ work_email: '[email protected]'}] }
]
}
{
'name' : 'entry 2'
'project' :
[
{companyName : 'another name', contactPerson : [{ work_email: '[email protected]'}] } ,
{companyName : 'another name1', contactPerson : [{ work_email: '[email protected]'}] } ,
{companyName : 'another name 2', contactPerson : [{ work_email: '[email protected]'}] }
]
}
What i want is to find the companyName that belongs to a given work_email. So if the work_email is [email protected]
the company name that should be returned should be 'a name'
So the query i built with mongoose is this :
const projects = await ClientManagers.findOne({'project.contactPerson.work_email' : '[email protected]'} , 'project.companyName');
But this is returning all company names (from entry one) not the single one i am looking for.
Upvotes: 2
Views: 279
Reputation: 49945
Filtering will always return whole document. You need to use projection to "reshape" it. You can consider the $ (projection) operator:
const projects = await ClientManagers.findOne({'project.contactPerson.work_email' : '[email protected]'} , { 'project.$': 1 });
Upvotes: 1