MongoDB嵌入式文档自我查询

时间:2014-07-22 11:45:58

标签: mongodb

  

您好,

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  :   []

}

  

我想在特定的经理

下找到子员工姓名      

可以请任何人帮助查询将如何,我正在尝试嵌套查询,但它不起作用。

     

谢谢&问候   阿贾伊

1 个答案:

答案 0 :(得分:0)

  

问题已经解决,我修改了EmployeeAndManager struture a   在Child Embedded文档中使用ManagerEmployeeID

{

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  :   []

}
  

,MongoDB查询现在看起来如下

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