Navigation
This manual pertains to an upcoming release, and is made available for testing purposes only.

Filter Documents in the Visualization

To display a subset of results in your data that match a given criteria, use the Filters input bar above the chart display. Input a filter document employing the same syntax used in the query portion of the db.collection.find() method. After entering a filter document, click Apply to see the filter reflected in your visualization.

Note

Filters on large collections may have performance issues if there are not appropriate indexes on the collection.

Basic Filter Example

The following chart shows the average runtime of movies by genre. The filter of {runtime: {$gte: 120 }} means that we are only taking into account movies which have a runtime greater than or equal to 120 minutes.

Charts dashboard filter

Regular Expression (RegEx) Filter

RegEx filters allow you to apply a regular expression as the match criteria to restrict the data MongoDB Charts displays. The expression uses the following syntax:

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

The $options are optional and are the same as the $regex options in the MongoDB shell.

Example

Filter data to document fields that start with a specific letter

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

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

Filter data to document fields that start with a specific letter ignoring case

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

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

Note

The quotation marks around the regular expression are required. You may not use forward slashes to delineate the regex value as you may in the MongoDB shell.

Relative Date Filter

Relative Date Filters allow you to specify a date from which MongoDB Charts restricts the data displayed. For example, you can set a Relative Date Filter to display data from only the last month or last year. To create a filter spanning from the specified date to the current date, specify the number of milliseconds since the Unix epoch of January 1, 1970. Use this date in conjunction with a comparison query operator to set the inclusive or exclusive time range of the data displayed.

Example

The following Relative Date Filter returns documents that have a timestamp field which resolves to a date less than 30 days from the current date:

{ timestamp: {  $gt: Date(Date() - 30 * 24 * 60 * 60 * 1000 ) } }

First, the inner Date() constructor generates the current date in milliseconds since the Unix epoch of January 1, 1970. The mathematical series 30 * 24 * 60 * 60 * 1000 results in the number of milliseconds elapsed in 30 days. The filter takes the current date in milliseconds since the Unix epoch and subtracts the number of milliseconds in 30 days. This results in a new millisecond value which the filter passes to an outer Date() constructor and resolves to the date 30 days prior to the time the user executes the filter.

Using a mathematical series as shown here allows the filter to always span a relative timeframe of 30 days prior to the time the user executes the filter.

For a more complete example of a relative date filter, see the Relative Date Filter Example.

ISO-8601 Dates

The date functions utilized in MongoDB Charts filters are consistent and compatible with the date functions used in the mongo shell. As a result, you can also use the ISODate() constructor in your Charts query filters. Specifying an ISODate() constructor with no parameters exhibits the same behavior as specifying a Date() constructor with no paramters, both returning the current date in their respective formats.

For example, the following filter returns documents that have a timestamp field between January 1, 2017 and December 31, 2017 inclusively:

{$and: [{timestamp: {$gte: ISODate("2017-01-01")}}, {timestamp: {$lte: ISODate("2017-12-31")}}]}

Relative Date Filter Example

The following chart visualizes workout data. Each document in the collection represents an individual workout activity, which contains information such as the type of workout and various exercise statistics. This line chart shows the distance per month covered across all workouts over the past year (365 days):

Charts dashboard filter

The chart utilizes the following filter:

{ "Workout Date (As Date)": {$gte: Date(Date() - 365 * 24 * 60 * 60 * 1000 ) }}

This filter returns documents where the Workout Date (As Date) field is within one year prior to the time Charts executes the query. The (Date() - 365 * 24 * 60 * 60 * 1000 ) parameter results in the date one year prior to the current date as expressed in milliseconds since the Unix epoch. Charts returns documents with a value greater than or equal to this date, as signified by the $gte operator.