|
| 1 | +.. _filter-embedded-dashboards: |
| 2 | + |
| 3 | +========================== |
| 4 | +Filter Embedded Dashboards |
| 5 | +========================== |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 2 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +You can customize your embedded dashboards by appending various |
| 16 | +query parameters to their iframe URLs or using the ``filter`` option |
| 17 | +with the |charts-short| Embedding SDK. |
| 18 | + |
| 19 | +.. _specify-filter-fields-dashboards: |
| 20 | + |
| 21 | +Specify Filterable Fields |
| 22 | +------------------------- |
| 23 | + |
| 24 | +A dashboard :ref:`Author <dashboard-roles>` specifies the fields that can be |
| 25 | +included in filters set by the embedding application code or added by |
| 26 | +dashboard viewers. A dashboard author can limit access to data by allowing only |
| 27 | +certain fields to be filtered. By default, no fields are allowed, |
| 28 | +meaning the dashboard cannot be filtered until you explicitly allow at |
| 29 | +least one field. |
| 30 | + |
| 31 | +To define filterable fields: |
| 32 | + |
| 33 | +1. For the desired :ref:`dashboard <dashboards>`, click the |
| 34 | + :icon-fa5:`ellipsis-h` button and select :guilabel:`Embed` from the |
| 35 | + dropdown. |
| 36 | + |
| 37 | +#. In the :guilabel:`Allowed filter fields` section, click the |
| 38 | + :icon-fa5:`pen` button. |
| 39 | + |
| 40 | + .. note:: |
| 41 | + |
| 42 | + This option only appears if you already have **Unauthenticated** |
| 43 | + or **Authenticated** embedding access enabled. |
| 44 | + |
| 45 | + You can specify on which fields dashboard viewers can filter data by: |
| 46 | + |
| 47 | + - Using the dropdown to select the fields |
| 48 | + - Manually typing values to add fields not listed in the dropdown |
| 49 | + - Selecting :guilabel:`Allow all fields in the data source used in |
| 50 | + this dashboard` |
| 51 | + |
| 52 | +#. When you have selected all desired fields, click :guilabel:`Save` |
| 53 | + below the dropdown. |
| 54 | + |
| 55 | +Dashboard :ref:`viewers <dashboard-roles>` and applications which render |
| 56 | +the dashboard can now use filters based on the specified fields to |
| 57 | +display subsets of the original dashboard data. If a viewer attempts to |
| 58 | +use a filter for a field not included in the :ref:`Allowed filter fields |
| 59 | +<specify-filter-fields-dashboards>` list, |charts| returns an |
| 60 | +:ref:`error <embedded-errors>`. |
| 61 | + |
| 62 | +Filters for Embedded Documents |
| 63 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 64 | + |
| 65 | +When you add a field to the :guilabel:`Allowed filter fields` list |
| 66 | +whose value is an embedded document, you must also specify each |
| 67 | +individual sub-field you want to allow. |
| 68 | + |
| 69 | +.. example:: |
| 70 | + |
| 71 | + Consider the following document: |
| 72 | + |
| 73 | + .. code-block:: json |
| 74 | + |
| 75 | + { |
| 76 | + "name": "Alice", |
| 77 | + "favorites" : |
| 78 | + { |
| 79 | + "color": "green", |
| 80 | + "animal": "turtle", |
| 81 | + "season": "autumn" |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + If you only add the ``favorites`` field to the list of allowed fields, |
| 86 | + it does *not* grant viewers permission to filter upon any of the |
| 87 | + sub-fields of ``favorites``. Instead, you may add one or more of the |
| 88 | + sub-fields to the list individually by specifying ``favorites.color``, |
| 89 | + ``favorites.animal``, or ``favorites.season``. |
| 90 | + |
| 91 | +.. _embed-options-filter-dashboards: |
| 92 | + |
| 93 | +Filter Data on Dashboards Embedded in an iframe |
| 94 | +----------------------------------------------- |
| 95 | + |
| 96 | +Use the ``filter`` query parameter to only display data that matches a |
| 97 | +specified :abbr:`MQL (MongoDB Query Language)` filter in your |
| 98 | +dashboard embedded in an iframe. |
| 99 | + |
| 100 | +You can only use the ``filter`` query parameter on the |
| 101 | +:guilabel:`Unauthenticated` dashboard. With unauthenticated dashboards, |
| 102 | +the dashboard :ref:`Author <dashboard-roles>` specifies the fields that |
| 103 | +can be included in filters set by the embedding application code or |
| 104 | +added by dashboard viewers. To learn how to specify filterable fields, |
| 105 | +see :ref:`Specify Filterable Fields <specify-filter-fields-dashboards>`. |
| 106 | + |
| 107 | +Filter Syntax |
| 108 | +~~~~~~~~~~~~~ |
| 109 | + |
| 110 | +You can specify an MQL document as your ``filter`` query |
| 111 | +parameter provided that the fields used in your filter are in |
| 112 | +the :ref:`list of allowed filterable fields <specify-filter-fields-dashboards>`. |
| 113 | + |
| 114 | +.. include:: /includes/fact-embedded-filter-examples-unauth.rst |
| 115 | + |
| 116 | +.. note:: |
| 117 | + |
| 118 | + You must URL-encode special characters of the filter |
| 119 | + parameter. |
| 120 | + |
| 121 | +Example |
| 122 | +``````` |
| 123 | + |
| 124 | +The following iframe ``src`` URL renders a dashboard which only displays |
| 125 | +documents with an ``imdb.rating`` greater than or equal to ``8``: |
| 126 | + |
| 127 | +.. code-block:: none |
| 128 | + :emphasize-lines: 3 |
| 129 | + |
| 130 | + https://charts.mongodb.com/charts-atlasproject1-piocy/embed/dashboards? |
| 131 | + id=93584ddb-1115-4a12-afd9-5129e47bbb0d& |
| 132 | + filter={"imdb.rating":%20{$gte:%208}} |
| 133 | + |
| 134 | + |
| 135 | +The URL uses an encoded ``filter`` parameter of |
| 136 | +``{"imdb.rating":%20{$gte:%208}}``. Decoded, |
| 137 | +this filter is: |
| 138 | + |
| 139 | +.. code-block:: json |
| 140 | + |
| 141 | + {"imdb.rating": {$gte: 8}} |
| 142 | + |
| 143 | +.. _filter-embedded-sdk-dashboards: |
| 144 | + |
| 145 | +Filter Data on Dashboards Embedded with the SDK |
| 146 | +----------------------------------------------- |
| 147 | + |
| 148 | +You can add a :ref:`filter <embed-options-filter-dashboards>` to an |
| 149 | +embedded dashboard with the ``filter`` option. Filtering allows the |
| 150 | +dashboard author to only display data in the embedded dashboard which |
| 151 | +matches a specified :abbr:`MQL (MongoDB Query Language)` filter. |
| 152 | + |
| 153 | +In the :guilabel:`Embed` modal, you must specify any fields included in |
| 154 | +the filter. The :guilabel:`Embed` modal contains a dropdown menu of |
| 155 | +fields on which to allow filtering. |
| 156 | + |
| 157 | +The following uses the ``filter`` option to represent only documents in |
| 158 | +which the ``total`` field is greater than ``100``: |
| 159 | + |
| 160 | +.. code-block:: javascript |
| 161 | + |
| 162 | + createDashboard({ |
| 163 | + baseUrl: '<your-base-url>', |
| 164 | + dashboardId: '<your-dashboard-id>', |
| 165 | + width: 500, |
| 166 | + height: 500, |
| 167 | + filter: { "total": { "$gt": 100 } } |
| 168 | + }) |
| 169 | + |
| 170 | +.. _inject-filter-per-user-dashboard: |
| 171 | + |
| 172 | +Inject User-Specific Filters |
| 173 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 174 | + |
| 175 | +When you embed a dashboard that requires :guilabel:`Authenticated` |
| 176 | +access, you can use the :guilabel:`Injected function` setting to inject |
| 177 | +a MongoDB filter document specific to each user who views the dashboard. |
| 178 | +The function has access to your Embedding Authentication Provider's |
| 179 | +token via ``context.token``, and can filter the dashboard data based on |
| 180 | +the token. |
| 181 | + |
| 182 | +This filter ensures that viewers of an embedded dashboard only see their |
| 183 | +own data, which is useful when embedding dashboard with potentially |
| 184 | +sensitive information. |
| 185 | + |
| 186 | +.. note:: |
| 187 | + |
| 188 | + If you use an {+atlas-app-services+} authentication provider, ``context.token`` |
| 189 | + contains the {+app-services+} |
| 190 | + :atlas:`user object </app-services/authentication/user-objects/>` to |
| 191 | + filter. For example, if you enable |
| 192 | + :atlas:`Custom User Data </app-services/users/enable-custom-user-data/>` |
| 193 | + for {+app-services+} users, the user object is available in |
| 194 | + ``context.token.custom_data``. |
| 195 | + |
| 196 | +To inject a filter specific to each user, in the |
| 197 | +:guilabel:`Authenticated` tab of the :guilabel:`Embed` dialog, set the |
| 198 | +:guilabel:`Injected function`setting to :guilabel:`On`. Specify a |
| 199 | +function and click :guilabel:`Save`. |
| 200 | + |
| 201 | +.. include:: /includes/example-user-filter-function.rst |
0 commit comments