Custom Binning Ranges
Hi Tom (and Team). We would love the ability to bin by custom ranges. We currently can only set a static number, which can lead to some pretty non-standard groupings of data.
EG: For Age, we'd like to be able to create the following bins: 0-17, 18-25, 26-35, 36-45, etc... Since under 18 is a common age range to group together, the bins we have to used are 0-17, 18-34, 35-52, etc..., which isn't a terribly standard age range to group data by.
-
AdminTom (Product Manager, MongoDB) commented
Hi Brian -
That's a good idea. We will be looking at enhancing binning next year so I'll see if we can do something along those lines.
In the meantime, it is possible to achieve what you're looking for with a calculated field. For example you could create a field with a definition like this:
{
$switch: {
branches: [
{ case: { $lt: [ '$age, 18 ] }, then: "0-18" },
{ case: { $lt: [ '$age', 30 ] }, then: "18-30" },
],
default: "30+"
}
}One thing to consider is that the resulting labels will be strings, so you'll need to choose names that put them in the right order with an alphabetical sort.