@@ -21,77 +21,197 @@ query parameters to their iframe URLs.
21
21
22
22
.. _embed-options-filter:
23
23
24
- Specify a Filter
25
- ----------------
24
+ Filter Data on Embedded Charts
25
+ ------------------------------
26
26
27
27
Use the ``filter`` query parameter to only display data in your
28
- embedded chart which matches a specified :abbr:`MQL (MongoDB Query Language)`
29
- filter.
28
+ embedded chart which matches a specified
29
+ :abbr:`MQL (MongoDB Query Language)` filter.
30
30
31
- .. important::
31
+ You can use the ``filter`` query parameter on both Unauthenticated
32
+ charts and charts which require a Verified Signature. The filtering
33
+ behavior differs with each authentication setting:
32
34
33
- For security reasons, the ``filter`` query parameter can only be used
34
- on charts embedded with a
35
- :ref:`Verified Signature <embedding-procedure>`. You should generate
36
- your ``filter`` in your server-side code, rather than receiving
37
- it from the client. Generating the filter from the server
38
- prevents the client from modifying the filter and potentially
39
- accessing restricted data.
35
+ - With Unauthenticated charts, the chart
36
+ :ref:`Author <dashboard-roles>` specifies the fields that can be
37
+ included in filters set by the embedding application code or added by
38
+ chart viewers. To learn how to specify filterable fields, see
39
+ :ref:`Specify Filterable Fields <specify-filter-fields>`.
40
40
41
- Syntax
42
- ~~~~~~
41
+ - With charts which require a Verified Signature, all document
42
+ fields can be filtered upon, however you must generate
43
+ the filter in the server-side code and include the filter as part of
44
+ your signed payload.
43
45
44
- Specify an MQL document as your ``filter`` query parameter. Use the
45
- same syntax used in the :ref:`query bar <query-bar>` in the Chart
46
- Builder.
46
+ .. _specify-filter-fields:
47
47
48
- MQL queries contain characters that must be URL-encoded before
49
- your server-side code calculates the verified signature.
50
- When |charts-short| verifies the signature, it URL-encodes the filter
51
- again using the JavaScript
52
- `encodeURIComponent
53
- <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent>`__
54
- function. You must use the same encoding algorithm to encode your
55
- filter.
48
+ Specify Filterable Fields (Unauthenticated Charts Only)
49
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56
50
57
- .. important::
51
+ A chart :ref:`Author <dashboard-roles>` specifies the fields that can be
52
+ included in filters set by the embedding application code or added by
53
+ chart viewers. Use these fields to restrict the data that viewers can
54
+ access. By default, no fields are whitelisted, meaning the chart cannot
55
+ be filtered until you explicitly whitelist at least one field.
58
56
59
- You must encode spaces in your filter as ``%20``, rather than ``+``
60
- or a raw space.
57
+ To define filterable fields:
61
58
62
- To see how correctly encode an MQL filter using different server-side
63
- programming languages, see `MongoDB Charts Embedding Examples
64
- <https://github.com/mongodb/charts-embedding-examples>`__ on GitHub.
59
+ 1. Navigate to the :ref:`dashboard <dashboards>` that contains the chart
60
+ where you wish to define filterable fields.
65
61
66
- Example
67
- ~~~~~~~
62
+ #. For the desired chart, click the :icon-fa5:`ellipsis-h` button and
63
+ select :guilabel:`Embed Chart` from the dropdown.
64
+
65
+ #. In the :guilabel:`User Specified Filters` section, use the
66
+ dropdown to select which fields chart viewers can use filter data
67
+ in the chart. You can also manually type values to add fields not
68
+ listed in the dropdown.
69
+
70
+ .. note::
71
+
72
+ This option only appears if you already have
73
+ :guilabel:`Unauthenticated` embedding access enabled.
74
+
75
+ #. When you have selected all desired fields, click :guilabel:`Save`
76
+ below the dropdown.
77
+
78
+ Chart :ref:`viewers <dashboard-roles>` and applications which render
79
+ the chart can now specify filters based on the whitelisted fields to
80
+ display subsets of the original chart data. If a viewer attempts to
81
+ specify a filter using a field not in the whitelist, an
82
+ :ref:`error <embedded-errors>` is returned.
83
+
84
+ .. topic:: Filters for Embedded Documents
85
+
86
+ When you whitelist a field whose value is an embedded document, you
87
+ must specify each individual sub-field you wish to add to the
88
+ whitelist.
89
+
90
+ .. example::
91
+
92
+ Consider the following document:
93
+
94
+ .. code-block:: json
95
+
96
+ {
97
+ "name": "Alice",
98
+ "favorites" :
99
+ {
100
+ "color": "green",
101
+ "animal": "turtle",
102
+ "season": "autumn"
103
+ }
104
+ }
105
+
106
+ If you only add the ``favorites`` field to the whitelist, it does
107
+ *not* grant viewers permission to filter upon any of the sub-fields
108
+ of ``favorites``. Instead, you may add one or more of the sub-fields
109
+ individually to the whitelist by specifying ``favorites.color``,
110
+ ``favorites.animal``, or ``favorites.season``.
111
+
112
+
113
+
114
+ Filter Syntax
115
+ ~~~~~~~~~~~~~
116
+
117
+ Select the appropriate tab to see an example of how to filter data in
118
+ an Unauthenticated chart and a Verified Signature chart:
119
+
120
+ .. tabs::
121
+
122
+ tabs:
123
+ - id: unauthenticated
124
+ name: Unauthenticated External Sharing
125
+ content: |
126
+
127
+ You can specify an MQL document as your ``filter`` query
128
+ parameter provided that the fields used in your filter are in
129
+ the
130
+ :ref:`whitelist of filterable fields <specify-filter-fields>`.
131
+
132
+ .. include:: /includes/fact-embedded-filter-examples-unauth.rst
133
+
134
+ .. note::
135
+
136
+ You must URL-encode special characters of the filter
137
+ parameter.
138
+
139
+ Example
140
+ ```````
141
+
142
+ The following iframe ``src`` URL renders a chart which only
143
+ displays documents with an ``imdb.rating`` greater than or
144
+ equal to ``8``:
145
+
146
+ .. code-block:: none
147
+ :emphasize-lines: 4
148
+
149
+ https://charts.mongodb.com/charts-atlasproject1-piocy/embed/charts?
150
+ id=93584ddb-1115-4a12-afd9-5129e47bbb0d&
151
+ tenant=3397ee6d-5079-4a20-b097-cedd475220b5&
152
+ filter={"imdb.rating":%20{$gte:%208}}&
153
+ autorefresh=30
154
+
155
+ The URL uses an encoded ``filter`` parameter of
156
+ ``{"imdb.rating":%20{$gte:%208}}``. Decoded,
157
+ this filter is:
158
+
159
+ .. code-block:: json
160
+
161
+ {"imdb.rating": {$gte: 8}}
162
+
163
+ - id: verified-signature
164
+ name: Verified Signature Required
165
+ content: |
166
+
167
+ Specify an MQL document as your ``filter`` query parameter.
168
+
169
+ .. include:: /includes/fact-embedded-filter-examples-verified.rst
170
+
171
+ When using filters in a Verified Signature, MQL queries contain
172
+ characters that must be URL-encoded before your server-side
173
+ code calculates the signature. When |charts-short| verifies the
174
+ signature, it URL-encodes the filter again using the JavaScript
175
+ `encodeURIComponent
176
+ <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent>`__
177
+ function. You must use the same encoding algorithm to encode
178
+ your filter.
179
+
180
+ .. important::
181
+
182
+ You must encode spaces in your filter as ``%20``, rather than ``+``
183
+ or a raw space.
184
+
185
+ To see how correctly encode an MQL filter using different server-side
186
+ programming languages, see `MongoDB Charts Embedding Examples
187
+ <https://github.com/mongodb/charts-embedding-examples>`__ on GitHub.
68
188
69
- The following iframe ``src`` URL renders a chart which only displays
70
- documents with an ``imdb.rating`` greater than or equal to ``8``:
189
+ Example
190
+ ```````
71
191
72
- .. code-block:: none
73
- :emphasize-lines: 6
192
+ The following iframe ``src`` URL renders a chart which only
193
+ displays documents with an ``imdb.rating`` greater than or
194
+ equal to ``8``:
74
195
75
- https://charts.mongodb.com/charts-atlasproject1-piocy/embed/charts?
76
- id=93584ddb-1115-4a12-afd9-5129e47bbb0d&
77
- tenant=3397ee6d-5079-4a20-b097-cedd475220b5&
78
- timestamp=1564156636&
79
- expires-in=300&
80
- filter=%7B%22imdb.rating%22%3A%20%7B%24gte%3A%208%7D%7D&
81
- autorefresh=30&
82
- signature=8e0d92b33934c928f6c6974e2f0102ace77f56d851cb0d33893e84c359ab1043
196
+ .. code-block:: none
197
+ :emphasize-lines: 6
83
198
84
- The URL uses an encoded ``filter`` parameter of
85
- ``%7B%22imdb.rating%22%3A%20%7B%24gte%3A%208%7D%7D``. Decoded, this
86
- filter is:
199
+ https://charts.mongodb.com/charts-atlasproject1-piocy/embed/charts?
200
+ id=93584ddb-1115-4a12-afd9-5129e47bbb0d&
201
+ tenant=3397ee6d-5079-4a20-b097-cedd475220b5&
202
+ timestamp=1564156636&
203
+ expires-in=300&
204
+ filter=%7B%22imdb.rating%22%3A%20%7B%24gte%3A%208%7D%7D&
205
+ autorefresh=30&
206
+ signature=8e0d92b33934c928f6c6974e2f0102ace77f56d851cb0d33893e84c359ab1043
87
207
88
- .. code-block:: json
208
+ The URL uses an encoded ``filter`` parameter of
209
+ ``%7B%22imdb.rating%22%3A%20%7B%24gte%3A%208%7D%7D``. Decoded,
210
+ this filter is:
89
211
90
- {"imdb.rating": {$gte: 8}}
212
+ .. code-block:: json
91
213
92
- The server-side code creates a payload with the necessary information
93
- to render the chart, including the filter. The payload ensures that
94
- the verified signature is valid before rendering the chart.
214
+ {"imdb.rating": {$gte: 8}}
95
215
96
216
.. _embed-options-refresh:
97
217
0 commit comments