Darshan Hiranandani : How can I use the outcome of a previous aggregation step to establish bucket boundaries?

Hello,

I need to group a history of operations into buckets, where each bucket should include all operations between a creation and deletion. Currently, I have two separate aggregation pipelines: one for generating the bucket boundaries and another for creating the buckets from those boundaries. I’m using Java to interact with MongoDB.

I’m trying to combine these into a single pipeline. My approach involves using a $lookup after generating the boundaries and calling the $bucket operation within the $lookup pipeline. I’m using $let to inject a variable for accessing the bucket boundaries, but I’m struggling with how to properly inject and set these boundaries.

Regards
Darshan Hiranandani

I’ve attempted to set the boundaries using $wantedBoundaries in various ways, including using $expr, but haven’t been successful. The documentation mentions that $expr is only mandatory for $match.

Could you help clarify whether it’s possible to achieve this within a single pipeline and how to correctly inject the boundaries for the $bucket stage?

Thank you.

Please follow up on your other threads as a courtesy to those who read, analyzed and replied on your issues.