Keep paid line items and calculate their totals
Load the practice orders and open keplergrid_transform_lab.orders on a Read only connection. Select Aggregation → JSON, paste this pipeline, and choose Run pipeline.
[
{ "$sort": { "_id": 1 } },
{ "$set": {
"paidLines": {
"$filter": {
"input": { "$cond": [{ "$isArray": "$lines" }, "$lines", []] },
"as": "line",
"cond": { "$eq": ["$$line.status", "paid"] }
}
}
} },
{ "$set": {
"paidLines": {
"$map": {
"input": "$paidLines",
"as": "line",
"in": {
"sku": "$$line.sku",
"lineTotal": { "$multiply": ["$$line.quantity", "$$line.unitPrice"] }
}
}
}
} }
]
The first $set keeps matching elements in their original order. The second reshapes each retained element to contain its SKU and calculated total. The two stages share paidLines so you can inspect the intermediate array.
Compare the arrays in KeplerGrid
In Stages, select stage 2 to see the full paid elements, then select stage 3 to see the smaller objects. ORD-101 keeps only NT-01 with lineTotal of 8.00. ORD-102 keeps AP-02 and NB-05, in that order.
All five parent orders remain. The other three show paidLines: []. The $isArray guard deliberately treats absent, null, and legacy scalar inputs as empty arrays for this report. Keep the original lines field visible when you need to tell those cases apart.
Use a follow-up $match on paidLines.0 with $exists: true if your report should contain only orders with a retained line.
See how to match whole documents with an array condition or how to make one result row per array element.
Next, Sum MongoDB array items with $reduce.
Next, Split and clean MongoDB strings with $split and $trim.
Next, Turn MongoDB object keys into rows with $objectToArray.