首页 > 解决方案 > 使用查找、匹配的 mongodb 聚合查询

问题描述

我尝试编写一个查询,其中集合 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", 

}

标签: mongodbmongoose

解决方案


推荐阅读