Skip to content

Commit be81ede

Browse files
authored
DOCSP-12234: [Charts] lookup fields (#417) (#419)
1 parent 48504ce commit be81ede

File tree

5 files changed

+127
-2
lines changed

5 files changed

+127
-2
lines changed

source/add-lookup-field.txt

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
.. _add-lookup-field:
2+
3+
==================
4+
Add a Lookup Field
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+
Lookup fields allow you to create a chart that joins data from multiple
16+
collections in the same database. A lookup field brings in documents from a
17+
second collection whose values correspond to a field in your chart's main
18+
data source.
19+
20+
Lookup fields are useful for leveraging parent/child and primary key/foreign
21+
key relationships between collections, or any situation in which a field
22+
in one collection references a field in another collection. Lookup fields
23+
use :manual:`$lookup </reference/operator/aggregation/lookup>` to retrieve
24+
matching documents from a remote collection.
25+
26+
Create a New Lookup Field
27+
-------------------------
28+
29+
You can create a lookup field from any field in your chart's data source
30+
which is not an :ref:`embedded object <embedded-objects-arrays>` and which
31+
contains matching data with a field in another collection. The remote
32+
collection must be:
33+
34+
- An existing |charts-short| data source.
35+
- Part of the same database as the local collection.
36+
37+
.. note::
38+
39+
You can also use the local collection as a lookup field source.
40+
41+
To add a lookup field, mouse over an existing field and click on the
42+
:guilabel:`ellipsis (...)` to the right of the field name. Select
43+
:guilabel:`Lookup field` from the dropdown menu. A modal window appears:
44+
45+
.. figure:: /images/charts/lookup-modal.png
46+
:figwidth: 350px
47+
:alt: Lookup field modal window
48+
49+
Select the desired collection and field from the dropdown menus. The remote
50+
field must contain at least one document with data that matches the local
51+
field, or the lookup field will be empty.
52+
53+
You have the option to either return all matching documents from the foreign
54+
collection or only the first matching document. Returning all matching
55+
documents is recommended for one-to-many relationships, such as parent/child
56+
and primary key relationships. Returning only the first document is recommended
57+
for one-to-one and many-to-one relationships, such as reference data codes.
58+
If you want to return only the first matching document, check the
59+
:guilabel:`Return only first matching document` radio button.
60+
61+
|charts-short| suggests a name for the new field, but you can enter a name
62+
of your choosing if you prefer. Click :guilabel:`Save` to create the new
63+
field.
64+
65+
The new field appears with a :guilabel:`binoculars` icon, indicating that
66+
it is a lookup field.
67+
68+
.. important::
69+
70+
Be sure that any field you use as a lookup field is appropriately
71+
:manual:`indexed </indexes/>`. Lookup operations on an
72+
unindexed field in a large collection can cause significant performance
73+
issues or timeouts.
74+
75+
To remove a lookup field from your field panel, mouse over the lookup
76+
field and click on the :guilabel:`ellipsis (...)` to the right of the
77+
field name. Select :guilabel:`Remove field` from the dropdown menu.
78+
79+
Example
80+
-------
81+
82+
The following example uses two data sources, one called ``product_catalog``
83+
and one called ``orders``.
84+
85+
The ``product_catalog`` collection contains the following documents:
86+
87+
.. code-block:: json
88+
:copyable: false
89+
90+
{ "_id" : 76234, "item" : "21 inch monitor" }
91+
{ "_id" : 38921, "item" : "USB C cable" }
92+
{ "_id" : 21167, "item" : "keyboard" }
93+
{ "_id" : 90252, "item" : "60 GB external hard drive" }
94+
95+
The ``orders`` collection contains the following documents:
96+
97+
.. code-block:: json
98+
:copyable: false
99+
100+
{ "_id" : 1, "sku": 38921, "quantity": 50 }
101+
{ "_id" : 2, "sku": 21167, "quantity": 75 }
102+
{ "_id" : 3, "sku": 76234, "quantity": 15 }
103+
{ "_id" : 4, "sku": 21167, "quantity": 20 }
104+
105+
Records in the ``orders`` collection use the ``sku`` field to reference
106+
the ``_id`` field in the ``product_catalog`` collection.
107+
108+
The goal is to create a :ref:`column chart <column-bar-chart-ref>` showing
109+
the number of ordered items. The following chart uses ``orders`` as its
110+
data source. The lookup field ``sku_lookup_product_catalog`` is created
111+
from the ``orders.sku`` field. It uses the ``product_catalog`` collection
112+
as its remote data source and ``product_catalog._id`` as its remote field.
113+
114+
The chart uses ``product_catalog.item`` as its X axis and ``orders.quantity``
115+
as its Y axis.
116+
117+
.. figure:: /images/charts/lookup-chart.png
118+
:figwidth: 85%
119+
:alt: Lookup field example chart

source/convert-field-data-types.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Convert a Field's Data Type
3131
---------------------------
3232

3333
To convert a field's data type, click on that field and select
34-
:guilabel:`Convert type` from the :guilabel:`Ellipses (...)` menu.
34+
:guilabel:`Convert type` from the :guilabel:`Ellipsis (...)` menu.
3535

3636
.. figure:: /images/charts/convert-field-data-type.png
3737
:figwidth: 25%

source/images/charts/lookup-chart.png

312 KB
Loading

source/images/charts/lookup-modal.png

71.8 KB
Loading

source/reshape-data.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ data into a form that works best for your charts:
1414
a single calculated field.
1515

1616
:ref:`convert-field-data-types`
17-
Convert the data type of the fields in your collection to a different type. For example, convert numbers stored as strings to the number
17+
Convert the data type of the fields in your collection to a different
18+
type. For example, convert numbers stored as strings to the number
1819
data type.
1920

21+
:ref:`add-lookup-field`
22+
Use :manual:`$lookup </reference/operator/aggregation/lookup/>` functionality
23+
to add a field based on a query across multiple data sources.
24+
2025
:ref:`charts-agg-pipeline`
2126
Transform your collection's documents with an aggregation
2227
pipeline.
@@ -26,4 +31,5 @@ data into a form that works best for your charts:
2631

2732
/calculated-fields
2833
/convert-field-data-types
34+
/add-lookup-field
2935
/aggregation-pipeline

0 commit comments

Comments
 (0)