db.employees.insert(
[
{ "_id" : 1, "name" : "Dev", "status": "PASSED" , "age": 1 },
{ "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev" , "status": "PASSED" },
{ "_id" : 3, "name" : "Ron", "reportsTo" : "Eliot" , "age": 3,"status": "PASSED" },
{ "_id" : 4, "name" : "Andrew", "reportsTo" : "Eliot" , "age": 4,"status": "PASSED" },
{ "_id" : 5, "name" : "Asya", "reportsTo" : "Ron" , "status": "FAILED" },
{ "_id" : 6, "name" : "Dan", "reportsTo" : "Andrew" , "age": 6 ,"status": "FAILED"}
])
db.employees.aggregate( [
{
$graphLookup: {
from: "employees",
startWith: "$reportsTo",
connectFromField: "reportsTo",
connectToField: "name",
maxDepth: 4,
depthField: "depth",
as: "reportingHierarchy"
}
},
{
$project:
{
name:1,
reportsTo:1,
status:1,
"reportingHierarchy.name":1,
"reportingHierarchy.reportsTo":1,
"reportingHierarchy.status":1,
numPass:
{
$cond: [{ $eq: ["$status", "PASSED"]}, 1, 0 ]
},
"reportingHierarchy.numPass":
{
$cond: [{ $eq: ["$status", "PASSED"]}, 1, 0 ]
},
numFail:
{
$cond: [{ $eq: ["$status", "FAILED"]}, 1, 0 ]
},
"reportingHierarchy.numFail":
{
$cond: [{ $eq: ["$reportingHierarchy.status", "FAILED"]}, 1, 0 ]
},
}
}
] )
{ "_id" : 1, "name" : "Dev", "status" : "PASSED", "reportingHierarchy" : [ ], "numPass" : 1, "numFail" : 0 }{ "_id" : 2, "name" : "Eliot", "reportsTo" : "Dev", "status" : "PASSED", "reportingHierarchy" : [ { "name" : "Dev", "status" : "PASSED", "numPass" : 1, "numFail" : 0 } ], "numPass" : 1, "numFail" : 0 }{ "_id" : 3, "name" : "Ron", "reportsTo" : "Eliot", "status" : "PASSED", "reportingHierarchy" : [ { "name" : "Dev", "status" : "PASSED", "numPass" : 1, "numFail" : 0 }, { "name" : "Eliot", "reportsTo" : "Dev", "status" : "PASSED", "numPass" : 1, "numFail" : 0 } ], "numPass" : 1, "numFail" : 0 }{ "_id" : 4, "name" : "Andrew", "reportsTo" : "Eliot", "status" : "PASSED", "reportingHierarchy" : [ { "name" : "Dev", "status" : "PASSED", "numPass" : 1, "numFail" : 0 }, { "name" : "Eliot", "reportsTo" : "Dev", "status" : "PASSED", "numPass" : 1, "numFail" : 0 } ], "numPass" : 1, "numFail" : 0 }{ "_id" : 5, "name" : "Asya", "reportsTo" : "Ron", "status" : "FAILED", "reportingHierarchy" : [ { "name" : "Dev", "status" : "PASSED", "numPass" : 0, "numFail" : 0 }, { "name" : "Eliot", "reportsTo" : "Dev", "status" : "PASSED", "numPass" : 0, "numFail" : 0 }, { "name" : "Ron", "reportsTo" : "Eliot", "status" : "PASSED", "numPass" : 0, "numFail" : 0 } ], "numPass" : 0, "numFail" : 1 }{ "_id" : 6, "name" : "Dan", "reportsTo" : "Andrew", "status" : "FAILED", "reportingHierarchy" : [ { "name" : "Dev", "status" : "PASSED", "numPass" : 0, "numFail" : 0 }, { "name" : "Eliot", "reportsTo" : "Dev", "status" : "PASSED", "numPass" : 0, "numFail" : 0 }, { "name" : "Andrew", "reportsTo" : "Eliot", "status" : "PASSED", "numPass" : 0, "numFail" : 0 } ], "numPass" : 0, "numFail" : 1 }
Hi,
Could you please provide the expected output from the query, and the logic that leads to that output?
Also, can you please provide the MongoDB version that your are using?
Best Regards,
Ivan