使用查找,匹配的mongodb聚合查询

时间:2018-07-26 04:42:47

标签: mongodb mongoose

我试图编写一个查询,其中集合1(用户)由用户名,报告管理员,empId和集合2(报价集合)组成。我想要的结果是如果登录的人是经理,则需要显示那里创建的报价团队和自己创建的报价

 User.aggregate([
            { $match: { reportingManager: req.user.user.name } },
            {
                $lookup:
                {
                    from: 'Quotation',
                    localField: 'empId',
                    foreignField: 'salesCrm',
                    as: 'data'
                }
            },
            { $unwind: "$data" },
            {$match:{"data.salesCrm":{$in:[req.user.user.empId]}}}
        ]

我的用户集合

{ 
"_id" : ObjectId("5b569cb4e924600e208660d6"), 
"name" : "abc", 
"email" : "abc@knowledgew.com", 
"designation" : "Software Developer", 
"department" : "Software Developer", 
"empId" : "123", 
"mobileNo" : 24564121654.0, 
"password" : "$2a$10$5d/0XV.yq.w5.Ipwc2uvtuvK4Mxji0f1mUW8rBwlhu/QR8jpztoOu", 
"vertical" : "Education,Market Research,Manufacturing,IT/ITES,BFSI", 
"reportingManager" : "def", 
"level" : "L1"

}

我的报价单

{ 
"_id" : ObjectId("5b4f27f476d65f25cc8d80ba"), 
"clientId" : "VPKW00001", 
"clientName" : "sssssss", 
"quotationId" : "00001_1819_CG7JE", 
"pmCost" : NumberInt(34), 
"contactPerson" : "5b4310db728ebf030c447ebd", 
"fileDomain" : "Panel Interview", 
"clientDomain" : "Software", 
"fuzzyMatch" : "No", 
"fileEngineering" : NumberInt(34), 
"workingDays" : "34", 
"currency" : "GBP", 
"scopeOfWork" : "LZ6PKLY_prod2.png", 
"projectPlan" : "V1RK7BW_prod2.png", 
"projectType" : "nnnn", 
"projectValue" : 646.41, 
"salesCrm" : "123", 

}

0 个答案:

没有答案