Navigation
This documentation refers to the MongoDB Charts service in MongoDB Atlas. Read the on-premises documentation to learn how to use the MongoDB Charts on site.

Bin, Sort, and Limit Your Data

You can bin, sort, and limit data in your charts to highlight key aspects in your data.

Bin Data

MongoDB Charts supports binning both numeric and date fields in your data. Binning breaks continuous data into discrete groups called bins, with each bin containing a contiguous subset of the original values. For example, you might group users into bins by the decade they were born, or group timestamped calendar events by the month of their start date.

Continuous vs Discrete Data

Continuous data can occupy any value over a continuous range. Some examples of continuous data include height, temperature, or the time a person was born.

Alternatively, discrete data is data which can only take certain values, categorized into a classification. Examples of discrete data include eye color and the number of students in a class.

Bin Date Fields

When handling dates, this data often comes in a continuous form. It can be useful to split this data into a specific windows of time to uncover additional trends and patterns. MongoDB Charts supports the following bins for dates:

  • Year
  • Month
  • Week of Year
  • Date of the Month
  • Day of the Week
  • Hour
  • Minute
  • Second

To use binning for dates:

  1. Drag a date field (signified by a calendar icon in the Fields section of the Chart Builder) to a category encoding channel.

    By default, binning is enabled as indicated by the Binning On toggle.

  2. Use the dropdown to select the bin size for your dates.

  1. Toggle the Periodic setting as desired.

    • If enabled, MongoDB Charts creates bins relative to the next highest time period and repeats each bin to cover the timeframe spanned in the data field.
    • If disabled, the bins Charts creates do not repeat.

    Example

    Consider a date field containing data spanning 5 years, and a bin selection of Month. If Periodic is disabled, Charts adds the 60 months in the dataset to the visualization.

    Alternatively, if Periodic is enabled, Charts only plots the 12 calendar months, and visualizes the total aggregated results from each calendar month.

    Note

    The Periodic setting is always enabled when selecting a bin size of Day of the Week.

    The Periodic setting is always disabled when selecting a bin size of Year, because year is the largest time period available in Charts.

To disable binning, toggle the Binning setting to off. In this case, each date from the field is added to the visualization with no grouping performed.

Example

The following chart visualizes historical sales data for an office supply store. Every sale is represented by a document in the supplySales collection. Each document in the collection includes the date of the sale as salesDate and a list of items included in the sale as the items array.

The saleDate field contains continuous timestamp data. The field is binned by Date of the Month with the Periodic setting enabled. This means that over the course of the several years the data spans, MongoDB Charts plots the mean value of sales from each individual date of the month:

Charts binning by day example

Bin Numeric Fields

MongoDB Charts can also bin numeric fields from a continuous set into groups of a specified size. This

To use binning for numeric fields:

  1. Drag a numeric field (signified by the number sign icon in the Fields section of the Chart Builder) to a category encoding channel.

    By default, binning is enabled as indicated by the toggle stating Binning On.

  2. Enter the desired bin size in the Bin Size input.

To disable binning, toggle the Binning setting to off. In this case, each individual value from the field is added to the visualization, rather than being grouped into bins.

Example

The following chart visualizes historical sales data for an office supply store. Every sale is represented by a document in the supplySales collection. Each sale document contains a customer object, which contains information on the customer purchasing the item(s).

The following chart applies binning to the customer.age field. The age field contains continuous numeric data and is binned by decade. Each age value is placed into the appropriate bin and Charts counts the number of ages in each bin to provide a high-level overview of the age distrubution of store customers.

Charts numeric binning example

Empty Bins

When binning is enabled, Charts displays entries for empty bins within the minimum and maximum data range a chart displays.

The value Charts uses for empty bins depends on the aggregation function you choose:

Aggregation Function Inferred Value
count or distinct 0
All Other Functions null

Charts displays bins with null values differently based on chart type:

Chart Type How Charts Displays null Bins
Column and Bar Charts Bars with a height of 0. Charts doesn’t display data labels for null bins, even if enabled.
Line and Area Charts Linear interpolation, with no data marker on the null bins. Charts doesn’t display data labels for null bins, even if enabled.

Sort Data

By default, Charts sorts data based on the Value field in descending order.

Use the Sort By dropdown in the Chart Builder to sort by either the Category field or aggregated value.

To toggle between ascending or descending sort order, click the a-z button to the right of the Sort By dropdown.

Limit Data

You can apply a limit to the Category encoding channel to only include a specified number of categories in your visualization. The categories included are the first matching categories based on the sort order specified. Limiting data can be useful when visualizing data with so many categories it becomes difficult to create a meaningful chart.

Example

The following chart shows the average IMDb rating of movies from a particular country:

Movie ratings by country

The dataset contains movies from many different countries, but it would be most interesting to see which countries produce the highest-rated movies. We can accomplish this by applying a limit to only show countries with the 10 highest average ratings for movies.

Switch the Limit Results toggle to On and leave the Show input at the default value of 10. The chart is now much easier to understand, and we have a clear view of the countries with the highest-rated movies:

Movie ratings by country limited