-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix($graphLookup): Handle path-based fields on initial match object.
When the selector field is a path, create full object graph for the starting object to match.
- Loading branch information
Showing
2 changed files
with
138 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
test/stackoverflow-examples/nested-top-down-hierarchy-with-aggregation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { aggregate } from "../support"; | ||
|
||
// https://stackoverflow.com/a/79174521 | ||
describe("create nested top-down hierarchy using aggregation", () => { | ||
it("passes", () => { | ||
const relations = [ | ||
{ | ||
_id: "p_commerce_metrics_dtst_v1", | ||
category: "dataset", | ||
dependentOn: { | ||
metrics: ["net_booking_count_v1"] | ||
} | ||
}, | ||
// Doc 2: | ||
{ | ||
_id: "net_booking_count_v1", | ||
category: "metric", | ||
dependentOn: { | ||
metrics: ["cancelled_booking_count_v1", "gross_booking_count_v1"] | ||
} | ||
}, | ||
// Doc 3: | ||
{ | ||
_id: "cancelled_booking_count_v1", | ||
category: "metric", | ||
dependentOn: { | ||
measures: ["hb_cancel_measure_v1"] | ||
} | ||
}, | ||
// Doc 4: | ||
{ | ||
_id: "gross_booking_count_v1", | ||
category: "metric", | ||
dependentOn: { | ||
measures: ["hb_booking_measure_v1"] | ||
} | ||
}, | ||
// Doc 5 (Not dependentOn any other document _id. Dead End): | ||
{ | ||
_id: "hb_cancel_measure_v1", | ||
category: "measure", | ||
usedBy: { | ||
metrics: ["cancelled_booking_count_v1", "more_metrics"] | ||
} | ||
}, | ||
// Doc 6 (Not dependentOn any other document _id. Dead End): | ||
{ | ||
_id: "hb_booking_measure_v1", | ||
category: "measure", | ||
usedBy: { | ||
metrics: ["gross_booking_count_v1", "more_metrics"] | ||
} | ||
} | ||
]; | ||
|
||
const result = aggregate(relations, [ | ||
{ | ||
$match: { | ||
_id: "p_commerce_metrics_dtst_v1" | ||
} | ||
}, | ||
{ | ||
$graphLookup: { | ||
from: relations, | ||
startWith: "$dependentOn.metrics", | ||
connectFromField: "dependentOn.metrics", | ||
connectToField: "_id", | ||
depthField: "depth", | ||
as: "dependentOnMetrics" | ||
} | ||
}, | ||
{ | ||
$set: { | ||
dependentOn: { | ||
metrics: { | ||
$setUnion: [ | ||
{ | ||
$ifNull: ["$dependentOn.metrics", []] | ||
}, | ||
{ | ||
$reduce: { | ||
input: "$dependentOnMetrics.dependentOn.metrics", | ||
initialValue: [], | ||
in: { | ||
$setUnion: ["$$value", "$$this"] | ||
} | ||
} | ||
} | ||
] | ||
}, | ||
measures: { | ||
$setUnion: [ | ||
{ | ||
$ifNull: ["$dependentOn.measures", []] | ||
}, | ||
{ | ||
$reduce: { | ||
input: "$dependentOnMetrics.dependentOn.measures", | ||
initialValue: [], | ||
in: { | ||
$setUnion: ["$$value", "$$this"] | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
$unset: "dependentOnMetrics" | ||
} | ||
]); | ||
|
||
expect(result).toEqual([ | ||
{ | ||
_id: "p_commerce_metrics_dtst_v1", | ||
category: "dataset", | ||
dependentOn: { | ||
measures: ["hb_cancel_measure_v1", "hb_booking_measure_v1"], | ||
metrics: [ | ||
"net_booking_count_v1", | ||
"cancelled_booking_count_v1", | ||
"gross_booking_count_v1" | ||
] | ||
} | ||
} | ||
]); | ||
}); | ||
}); |