Skip to content

Commit 54d990b

Browse files
(DOCSP-5011): Filtering updates for numbers and strings (#168)
* (DOCSP-5011): Filtering updates for numbers and strings * Adding blurb for all other strings * Updates per Kay's feedback * Updates per Tom's feedback * Updates per Tom's feedback
1 parent 66d0924 commit 54d990b

File tree

2 files changed

+179
-119
lines changed

2 files changed

+179
-119
lines changed

source/filter-documents.txt

Lines changed: 179 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -9,181 +9,241 @@ Filter Documents in the Visualization
99
.. contents:: On this page
1010
:local:
1111
:backlinks: none
12-
:depth: 1
12+
:depth: 2
1313
:class: singlecol
1414

15-
To display a subset of results in your data that match a given
16-
criteria, use the :guilabel:`Filters` input bar above the chart
17-
display. Input a filter document employing the same syntax used in the
18-
query portion of the :manual:`db.collection.find()
19-
</tutorial/query-documents/>` method. After entering a filter document,
20-
click :guilabel:`Apply` to see the filter reflected in your visualization.
15+
Filters display a subset of results that match a given criteria.
16+
|charts| provides two ways to filter your data. You can either use:
2117

22-
.. note::
18+
- The :ref:`Filter Tab <filter-tab>` in the Chart Builter, or
2319

24-
Filters on large collections may have performance issues if there
25-
are not appropriate :manual:`indexes <indexes>` on the collection.
20+
- The :ref:`Query Bar <query-bar>` above the chart display.
2621

27-
Basic Filter Example
28-
--------------------
22+
.. _filter-tab:
2923

30-
The following chart shows the average runtime of movies by genre.
31-
The filter of ``{runtime: {$gte: 120 }}`` means that we are only
32-
taking into account movies which have a runtime greater than or
33-
equal to 120 minutes.
24+
Filter Tab
25+
----------
3426

35-
.. figure:: /images/charts/charts-dashboard-filter-2.png
36-
:figwidth: 720px
37-
:alt: Charts dashboard filter
27+
The chart builder contains a filter tab where you can drag and drop
28+
fields to specify filters based on those fields. To filter data using
29+
the filter tab:
3830

39-
.. _regex-filter:
31+
1. Click the center tab in the chart builder:
4032

41-
Regular Expression (RegEx) Filter
42-
---------------------------------
33+
.. figure:: /images/charts/filter-tab.png
34+
:scale: 60%
35+
:alt: Image showing how to access filter tab
4336

44-
.. _`regular expression`: https://en.wikipedia.org/wiki/Regular_expression?oldid=858335070
37+
|
4538

39+
#. Drag a field from the :guilabel:`Fields` on the left to the
40+
:guilabel:`Chart Filters` section of the tab.
41+
42+
The data type of the selected field dictates the available filtering
43+
options. You can filter fields with the following data types:
4644

47-
RegEx filters allow you to apply a `regular expression`_ as the match
48-
criteria to restrict the data |charts| displays. The expression
49-
uses the following syntax:
45+
- :ref:`Numeric <filter-numeric>`
5046

51-
.. code-block:: sh
47+
- :ref:`String <filter-string>`
5248

53-
{ <field>: { $regex: "pattern", $options: "<options>" } }
49+
.. note::
5450

55-
The ``$options`` are optional and are the same as the
56-
:query:`$regex options <$options>` in the MongoDB shell.
51+
You cannot use the same field for multiple filters.
5752

58-
.. example::
53+
.. _filter-numeric:
5954

60-
**Filter data to document fields that start with a specific letter**
55+
Filter Numeric Fields
56+
~~~~~~~~~~~~~~~~~~~~~
6157

62-
To find all documents where the ``jobs`` field begins with the
63-
letter ``A``, you would write the following in the
64-
:guilabel:`Filter` bar:
58+
When you drag a numeric field to the filter panel, you
59+
can filter based on minimum and/or maximum values for that field.
6560

66-
.. code-block:: javascript
61+
.. list-table::
62+
:header-rows: 1
6763

68-
{ "job" : { $regex : "^A" } }
64+
* - To specify a minimum value:
65+
- To specify a maximum value:
6966

70-
**Filter data to document fields that start with a specific letter
71-
ignoring case**
67+
* - 1. Toggle :guilabel:`Min` to :guilabel:`On`.
7268

73-
To find all documents where the ``jobs`` field begins with the
74-
letter ``A`` or ``a``, you would write the following in the
75-
:guilabel:`Filter` bar:
69+
#. Specify the desired minimum value.
70+
71+
#. Select whether this is an inclusive minimum value.
7672

77-
.. code-block:: javascript
73+
- 1. Toggle :guilabel:`Max` to :guilabel:`On`.
7874

79-
{ "job" : { $regex : "^A", $options : "i" } }
75+
#. Specify the desired maximum value.
76+
77+
#. Select whether this is an inclusive maximum value.
8078

81-
.. note::
79+
.. example::
8280

83-
The quotation marks around the regular expression are required. You
84-
may not use forward slashes to delineate the regex value as you may
85-
in the MongoDB shell.
81+
If you have a minimum value of ``5`` with the :guilabel:`Inclusive`
82+
setting on, |charts| shows documents where the field is greater than
83+
or equal to ``5``.
8684

87-
.. _date-filter:
85+
Alternatively, if :guilabel:`Inclusive` is off, |charts| shows
86+
documents where the field is greater than ``5``.
8887

89-
Relative Date Filter
90-
--------------------
88+
.. _filter-string:
9189

92-
Relative Date Filters allow you to specify a date from which |charts|
93-
restricts the data displayed. For example, you can set a Relative Date
94-
Filter to display data from only the last month or last year. To
95-
create a filter spanning from the specified date to the current date,
96-
specify the number of milliseconds since the Unix epoch of January 1,
97-
1970. Use this date in conjunction with a
98-
:manual:`comparison query operator </reference/operator/query-comparison/>`
99-
to set the inclusive or exclusive time range of the data displayed.
90+
Filter String Fields
91+
~~~~~~~~~~~~~~~~~~~~
10092

101-
.. example::
93+
When you drag a string field to the filter panel, |charts| displays a
94+
list of up to 20 distinct field values. If more than 20 distinct values
95+
exist, |charts| displays 20 randomly selected values.
10296

103-
The following Relative Date Filter returns documents that have a
104-
``timestamp`` field which resolves to a date less than 30 days from
105-
the current date:
97+
The list also includes:
10698

107-
.. code-block:: javascript
99+
- :guilabel:`NULL / MISSING` for documents with ``null`` values for
100+
the field or are missing the field.
108101

109-
{ timestamp: { $gt: new Date(new Date() - 30 * 24 * 60 * 60 * 1000 ) } }
102+
- :guilabel:`Empty String` for documents with ``""`` values for the
103+
field.
110104

111-
First, the inner ``new Date()`` constructor generates the current
112-
date in milliseconds since the Unix epoch of January 1, 1970. The
113-
mathematical series ``30 * 24 * 60 * 60 * 1000`` results in the
114-
number of milliseconds elapsed in 30 days. The filter takes the
115-
current date in milliseconds since the Unix epoch and subtracts the
116-
number of milliseconds in 30 days. This results in a new millisecond
117-
value which the filter passes to an outer ``new Date()`` constructor
118-
and resolves to the date 30 days prior to the time the user executes
119-
the filter.
105+
To include a value not displayed in the list, add a specific value by
106+
clicking :guilabel:`Add Value`.
120107

121-
Using a mathematical series as shown here allows the filter to
122-
always span a relative timeframe of 30 days prior to the time
123-
the user executes the filter.
108+
Select the string values to display in the chart. By default, all
109+
values are selected.
124110

125-
For a more complete example of a relative date filter, see the
126-
:ref:`Relative Date Filter Example <relative-date-example>`.
111+
.. tip::
127112

128-
.. admonition:: ISO-8601 Dates
129-
:class: note
113+
If all strings are selected, you can click :guilabel:`Deselect All`
114+
at the top of the list to hide all strings.
115+
116+
If not all strings are selected, you can click :guilabel:`Select All`
117+
to return to the default state of having all strings displayed.
130118

131-
The date functions utilized in |charts| filters are consistent and
132-
compatible with the date functions used in the
133-
:manual:`mongo shell </mongo>`. As a result, you can also use the
134-
``ISODate()`` constructor in your |charts-short| query filters.
135-
Specifying an ``ISODate()`` constructor with no parameters exhibits
136-
the same behavior as specifying a ``new Date()`` constructor with no
137-
paramters, both returning the current date in their respective
138-
formats.
119+
Display Strings Not in the List
120+
```````````````````````````````
121+
122+
You can check :guilabel:`All other values` to
123+
display all string values not included in the list.
124+
125+
- If :guilabel:`All other values` is checked, |charts| filters out
126+
any unchecked list items by using a :query:`$nin` query.
127+
128+
- If :guilabel:`All other values` is unchecked, |charts| only
129+
includes the checked list items by using an :query:`$in` query.
130+
131+
.. _query-bar:
139132

140-
.. example::
133+
Query Bar
134+
---------
135+
136+
To filter data using the :guilabel:`Query` bar:
141137

142-
The following filter returns documents that have a
143-
``timestamp`` field between January 1, 2017 and December 31, 2017
144-
inclusively:
138+
1. In the :guilabel:`Query` bar, input a filter document. Use the
139+
same syntax used in the query portion of the
140+
:manual:`db.collection.find()
141+
</reference/method/db.collection.find/>` method.
142+
143+
#. Click :guilabel:`Apply`.
145144

146-
.. code-block:: javascript
145+
.. note::
147146

148-
{$and: [{timestamp: {$gte: ISODate("2017-01-01")}},
149-
{timestamp: {$lte: ISODate("2017-12-31")}}]}
147+
Filters on large collections may have performance issues if there
148+
are not appropriate :manual:`indexes <indexes>` on the collection.
150149

151-
.. _relative-date-example:
150+
Examples
151+
~~~~~~~~
152152

153-
Relative Date Filter Example
154-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
153+
Comparison Query Filter
154+
```````````````````````
155155

156-
The following chart visualizes workout data. Each document in the
157-
collection represents an individual workout activity, which contains
158-
information such as the type of workout and various exercise
159-
statistics. This line chart shows the distance per month covered
160-
across all workouts over the past year (365 days):
156+
The following chart shows the average runtime of movies by
157+
genre. The filter of ``{runtime: {$gte: 120 }}`` means that we only
158+
include movies which have a runtime greater than or equal to 120
159+
minutes.
161160

162-
.. figure:: /images/charts/relative-date-example.png
161+
.. figure:: /images/charts/charts-dashboard-filter-2.png
163162
:figwidth: 720px
164163
:alt: Charts dashboard filter
165164

166-
The chart utilizes the following filter:
165+
.. _regex-filter:
166+
167+
Regular Expression (RegEx) Query
168+
````````````````````````````````
169+
170+
.. _`regular expression`: :manual:`
171+
172+
173+
Use the :query:`$regex` query operator to filter using a
174+
regular expression:
175+
176+
.. code-block:: javascript
177+
178+
{ <field>: { $regex: "pattern", $options: "<options>" } }
179+
180+
.. note::
181+
182+
You must enclose the pattern in quotes. You cannot use the ``/pattern/`` syntax.
183+
184+
For example, to find all documents where the ``jobs`` field begins with
185+
the letter ``A``, you would write the following in the
186+
:guilabel:`Query` bar:
187+
188+
.. code-block:: javascript
189+
190+
{ "job" : { $regex : "^A" } }
191+
192+
To find all documents where the ``jobs`` field begins with the
193+
letter ``A`` or ``a``, you would write the following in the
194+
:guilabel:`Query` bar:
167195

168196
.. code-block:: javascript
169197

170-
{ "Workout Date (As Date)": {$gte: new Date(new Date() - 365 * 24 * 60 * 60 * 1000 ) }}
198+
{ "job" : { $regex : "^A", $options : "i" } }
199+
200+
Date Range Query
201+
````````````````
202+
203+
The following query returns documents that have a
204+
``timestamp`` field between January 1, 2017 and December 31, 2017
205+
inclusively:
206+
207+
.. code-block:: javascript
171208

172-
This filter returns documents where the ``Workout Date (As Date)``
173-
field is within one year prior to the time |charts-short| executes the
174-
query. The ``(new Date() - 365 * 24 * 60 * 60 * 1000 )`` parameter
175-
results in the date one year prior to the current date as expressed in
176-
milliseconds since the Unix epoch. |charts-short| returns documents
177-
with a value greater than or equal to this date, as signified by the
178-
:manual:`$gte </reference/operator/query/gte/>` operator.
209+
{timestamp: {$gte: new Date("2017-01-01"), $lte: new Date("2017-12-31")}}
179210

180-
.. admonition:: Date() is not supported
211+
.. admonition:: ISO-8601 Dates
181212
:class: note
182213

183-
The ``Date()`` function (as opposed to the ``new Date()``
184-
constructor) returns the current date as a string, so it cannot be
185-
used for filtering dates in |charts-short|. Use:
214+
The date functions utilized in |charts| queries are consistent and
215+
compatible with the date functions used in the
216+
:manual:`mongo shell </mongo>`. As a result, you can use:
186217

187218
- ``new Date()``,
188219
- ``ISODate()``, or
189-
- ``new ISODate()``
220+
- ``new ISODate()``.
221+
222+
The ``Date()`` function (as opposed to the ``new Date()``
223+
constructor) returns the current date as a string, so it cannot be
224+
used for querying dates in |charts-short|.
225+
226+
.. _date-filter:
227+
228+
Relative Date Query
229+
```````````````````
230+
231+
You can include a mathematical expression in the ``new Date()``
232+
constructor to get a relative date, such as 1 month ago or 1 year
233+
ago.
234+
235+
For example, the following query returns documents that
236+
have a ``timestamp`` field greater than 30 days
237+
(30 * 24 * 60 * 60 * 1000 milliseconds) from the current date and time:
238+
239+
.. code-block:: javascript
240+
241+
{ timestamp: { $gt: new Date(new Date() - 30 * 24 * 60 * 60 * 1000 ) } }
242+
243+
Alternatively, you can specify the date components. For example, the
244+
following query returns documents that have a ``timestamp`` field
245+
greater than 1 month ago:
246+
247+
.. code-block:: javascript
248+
249+
{ timestamp: { $gt: new Date(new Date().getFullYear(), new Date().getMonth() -1 , new Date().getDate() ) } }

source/images/charts/filter-tab.png

37.2 KB
Loading

0 commit comments

Comments
 (0)