Q:

MongoDB Embedded Document Self Query

Hi,
I have a collections of EmployeeAndManager as below

{
EmployeeID      :   1
EmployeeName    :   Employee1
Role            :   Manager
EmployeesUnder  :   [
                {   
                    EmployeeId  :   2,
                    FromDate    :   12/12/2012
                },
                {   
                    EmployeeId  :   3,
                    FromDate    :   12/1/2012
                }   
            ]
EmployeeID  :   2
EmployeeName    :   Employee2
Role            :   Developer
EmployeesUnder  :   []
EmployeeID  :   3
EmployeeName    :   Employee3
Role            :   Developer
EmployeesUnder  :   []

}

I want to find the child Employee Names under the particular Manager
Could please anyone help how the query will be, i am trying nested query but it doesnot work.
Thanks & Regards
Ajay

A:

The issue is been solved, i modified the EmployeeAndManager struture a
  bit to have ManagerEmployeeID in the Child Embedded document

{

EmployeeID      :   1
EmployeeName    :   Employee1
Role            :   Manager
EmployeesUnder  :   [
                {   ManagerEmployeeID : 1
                    EmployeeId  :   2,
                    FromDate    :   12/12/2012
                },
                {   
                   ManagerEmployeeID : 1
                    EmployeeId  :   3,
                    FromDate    :   12/1/2012
                }   
            ]
EmployeeID  :   2
EmployeeName    :   Employee2
Role            :   Developer
EmployeesUnder  :   []
EmployeeID  :   3
EmployeeName    :   Employee3
Role            :   Developer
EmployeesUnder  :   []

}

and the MongoDB query now looks as below

db.EmployeeAndManager.find({"EmployeeID" : {"$in" :db.EmployeeAndManager.distinct('EmployeesUnder.EmployeeID',{"EmployeesUnder.ManagerEmployeeID":1})}})

