Hello, I would like to know if it’s possible (and if so, how to do it) to group entries by a certain key, and then bucket those same entries on another key, at the same time ?
For instance, I created this playground: Mongo playground
Here, I want to group entries by the user and bucket the sizes for each user, for instance with the boundaries [0; 10000; 100000], and get the number of entries in those slices.
For instance, here is what I expect the result to look like: Mongo playground
I’m not sure if it’s doable, I feel like it is using $facet, but not sure… The issue I have is that if I simply use a facet with a group on user, and do a bucket by size, the group will only a single accumulated value, and the bucket will fail to do what I want. For a similar reason, I can’t do these in two different steps, as either the ßize or the user will be lost with the accumulations.
Thanks for the help, turns out I don’t even need to use buckets at all then…
However, is there a simpler way to do this if you have more boundaries to take into account ? For instance, for the use case I have, there might be up to 10 boundaries, and writing all the conditions that way seems impossible for me…
I suppose a for loop might be usable if you define in an earlier set all the boundaries, but if you didn’t do it here, I suppose there’s something I’m missing.