Docs Menu

Filter Documents in the Visualization

On this page

Filters display a subset of results that match a given criteria. MongoDB Charts provides two ways to filter your data. You can either use:

The chart builder contains a filter tab where you can drag and drop fields to specify filters based on those fields. To filter data using the filter tab:

  1. Click the center tab in the chart builder:

    Image showing how to access filter tab

  2. Drag a field from the Fields on the left to the Chart Filters section of the tab.

The data type of the selected field dictates the available filtering options. You can filter fields with the following data types:

Note

You cannot use the same field for multiple filters.

When you drag a numeric field to the filter panel, you can filter based on minimum and/or maximum values for that field.

To specify a minimum value:
To specify a maximum value:
  1. Toggle Min to On.
  2. Specify the desired minimum value.
  3. Select whether this is an inclusive minimum value.
  1. Toggle Max to On.
  2. Specify the desired maximum value.
  3. Select whether this is an inclusive maximum value.
Example

If you have a minimum value of 5 with the Inclusive setting on, MongoDB Charts shows documents where the field is greater than or equal to 5.

Alternatively, if Inclusive is off, MongoDB Charts shows documents where the field is greater than 5.

When you drag a string or ObjectId field to the filter panel, MongoDB Charts displays a list of up to 20 distinct field values. If more than 20 distinct values exist, MongoDB Charts displays 20 randomly selected values.

The list also includes:

  • NULL / MISSING for documents with null values for the field or are missing the field.
  • Empty String for documents with "" values for the string field. (String fields only.)

Select the values to display in the chart. By default, all values are selected.

Tip

If all values are selected, you can click Deselect All at the top of the list to hide all values.

If not all values are selected, you can click Select All to return to the default state of having all values displayed.

To display a specific value not included in the list, add the value by clicking Add Value.

Important

For ObjectId fields, value you input must be a well-formed ObjectId, or else MongoDB Charts does not accept the value.

To display all other values not included in the list, check All other values.

  • If All other values is checked, MongoDB Charts filters out any unchecked list items by using a $nin query.
  • If All other values is unchecked, MongoDB Charts only includes the checked list items by using an $in query.

When you drag a date field to the filter panel, you can filter based on a specified date range. This range can either be:

  • A relative date range, which specifies a range relative to the time the chart is rendered (e.g., the last six months).
  • An absolute date range, which is a range between specific beginning and end dates.

Select the appropriate tab for more information on the date filter:

You can filter the chart data based on the boolean value of a specific field. Drag a boolean field to the filter panel to view the available options:

  • True displays documents where the field value is true.
  • False displays documents where the field value is false.
  • NULL / MISSING displays documents where the field is null or does not exist.

All options are selected by default. The chart preview updates as you modify the selected options. Deselecting all options renders a blank chart.

Example

A chart using the following filter only displays documents where the passed field is true. The chart does not display documents where the passed field is false, null, or does not exist.

Image showing boolean filter

You can toggle whether a filter in the Filter tab is enabled or disabled. Disabled filters do not affect the documents which appear in the chart. Disabled filters are grayed out and cannot be edited until they are enabled.

To toggle whether a filter is enabled, hover over a filter and set the appearing toggle at the top of the filter to the desired setting.

Image showing filter toggle

The Query bar supports more complex queries than the filter panel. Additionally, you can use the query bar to create aggregation pipelines to process your data before it is rendered.

To filter data using the Query bar:

  1. In the Query bar, input a filter document. Use the same syntax used in the query portion of the db.collection.find() method. Your filter document must be in curly brackets.
  2. Click Apply.

The following chart shows the average Metacritic ratings of movies over time binned by 5 year periods.

Example query comparison

The chart utilizes the following query:

{ 'writers.1': { $exists: true }}

writers is an array where each element is a writer who contributed to the movie. This filter ensures that only documents with at least two writers are factored into the mean Metacritic rating by checking that the second array element exists.

Use the $regex query operator to filter using a regular expression:

{ <field>: { $regex: "pattern", $options: "<options>" } }
Note

You must enclose the pattern in quotes. You cannot use the /pattern/ syntax.

For example, to find all documents where the jobs field begins with the letter A, you would write the following in the Query bar:

{ "jobs" : { $regex : "^A" } }

To find all documents where the jobs field begins with the letter A or a, you would write the following in the Query bar:

{ "jobs" : { $regex : "^A", $options : "i" } }

The following chart shows total sale amounts from an office supply company, categorized by purchase method:

Example date query

The chart utilizes the following query:

{
$and: [
{
saleDate: { $gte: new Date("2017-01-01") }
},
{
'items.4': { $exists: true }
} ]
}

Each document in the collection represents a single sale. items is an array where each element is an item purchased during a sale.

This query restricts the documents shown to only those with a saleDate equal to or more recent than January 1, 2017 with at least 5 elements in the items array.

  • Filters on large collections may encounter performance issues if the collection is not appropriately indexed.
  • The date functions utilized in the MongoDB Charts query bar are consistent and compatible with the date functions used in the mongo shell. As a result, you can use:

    • new Date(),
    • ISODate(), or
    • new ISODate().

    The Date() function (as opposed to the new Date() constructor) returns the current date as a string, so it cannot be used for querying dates in Charts.

Aggregation pipelines transform your documents into an aggregated set of results. In MongoDB Charts, aggregation pipelines are commonly used to visualize new fields created from calculated results of pre-existing fields, but also have many other applications.

To create an aggregation pipeline:

  1. In the Query bar, input an aggregation pipeline. Your pipeline must be in square brackets.
  2. Click Apply to execute your pipeline.

The following chart shows total sale amounts from an office supply company, categorized by store location. The chart utilizes the following aggregation pipeline in the Query bar:

[
{
$unwind: "$items"
},
{
$addFields: {
saleAmount: {
$multiply: [ "$items.price", "$items.quantity" ]
}
}
}
]

This aggregation pipeline processes the collection data using the following stages:

  1. The $unwind stage unwinds the items array and outputs a new document for each item in the array. Each element in the items array contains a single item sold during a transaction.
  2. The $addFields stage adds a new field to the documents called saleAmount. The $multiply expression sets the value of saleAmount to the product of items.price and items.quantity. You can see this new field highlighted in the following screenshot:
Example Aggregation Pipeline

Once the data has been processed using the pipeline, the chart displays the the Sum of all saleAmounts categorized by store location.

The $lookup operator is not supported in aggregation queries defined in the Chart Builder. However, you can use this operator in Data Source pipelines. To learn more about how to use pipelines to pre-process data before it reaches the Chart Builder, see Data Source Pipelines.

Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.