Skip to content

Commit a4b59d4

Browse files
(DOCSP-30632): Migrate calculate fields (#19)
* (DOCSP-30632): Migrate calculate fields * try formatting * apply new formatting * try formatting again * more formatting * fix remaining example formatting * add example explanations * typo * tpo * typo * wording * add remaining pages * edits * add on this page * edit * formatting * edit * formatting fix * address review comments
1 parent c78a5e6 commit a4b59d4

File tree

7 files changed

+559
-7
lines changed

7 files changed

+559
-7
lines changed

source/mapping-rules/calculated-fields/add-calculated-fields.txt

Lines changed: 376 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,379 @@
22

33
=====================
44
Add Calculated Fields
5-
=====================
5+
=====================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
Calculated fields let you create new fields in your documents by
14+
combining values from existing database columns using JavaScript
15+
expressions. Calculated fields are evaluated each time a row is
16+
processed during a sync job.
17+
18+
Before you Begin
19+
----------------
20+
21+
To create a calculated field, you must define a mapping rule.
22+
23+
To create mapping rules, see the following pages:
24+
25+
- :ref:`rm-new-rules-from-mappings`
26+
27+
- :ref:`rm-create-rule-to-mdb`
28+
29+
- :ref:`rm-create-mapping-rules`
30+
31+
About this Task
32+
---------------
33+
34+
Calculated field expressions access values from the current source
35+
database row using the syntax ``columns["<COLUMN_NAME>"]``.
36+
37+
Steps
38+
-----
39+
40+
#. From the :guilabel:`Mapping` screen, click a table or collection name
41+
on the :guilabel:`schema model` pane or diagram view.
42+
43+
#. Add a new mapping rule or edit an existing mapping rule.
44+
45+
#. Click the :guilabel:`+` icon to the right of the :guilabel:`All
46+
fields` label.
47+
48+
#. Define a name for the new field in the :guilabel:`Field Name` text
49+
box.
50+
51+
#. Define a valid JavaScript expression for the new field in the ``Value
52+
expression`` text box.
53+
54+
#. Click :guilabel:`Done`.
55+
56+
#. Click :guilabel:`Save and close`.
57+
58+
The new field is visible in MongoDB after your next sync job runs.
59+
60+
Examples
61+
--------
62+
63+
The following examples show JavaScript expressions that you can use in
64+
calculated fields:
65+
66+
Concatenate Strings
67+
~~~~~~~~~~~~~~~~~~~
68+
69+
Combine two columns into a single field.
70+
71+
The following example concatenates string values from the ``firstName``
72+
and ``lastName`` columns.
73+
74+
Expression:
75+
76+
.. code-block:: none
77+
78+
columns["firstName"] + ' ' + columns["lastName"]
79+
80+
Input:
81+
82+
.. list-table::
83+
:header-rows: 1
84+
:widths: 10 10
85+
86+
* - Column
87+
- Value
88+
89+
* - ``firstName``
90+
- ``John``
91+
92+
* - ``lastName``
93+
- ``Smith``
94+
95+
Output:
96+
97+
.. code-block:: none
98+
:copyable: false
99+
100+
"John Smith"
101+
102+
Split Strings
103+
~~~~~~~~~~~~~
104+
105+
Split column values into an array based on a specified character.
106+
107+
The following example splits the ``fullName`` column into an array using
108+
a space character as a delimiter, and returns the first element of the
109+
array.
110+
111+
Expression:
112+
113+
.. code-block:: none
114+
115+
columns["fullName"].split(' ')[0]
116+
117+
Input:
118+
119+
.. list-table::
120+
:header-rows: 1
121+
:widths: 10 10
122+
123+
* - Column
124+
- Value
125+
126+
* - ``fullName``
127+
- ``John Doe``
128+
129+
Output:
130+
131+
.. code-block:: none
132+
:copyable: false
133+
134+
"John"
135+
136+
Replace Strings
137+
~~~~~~~~~~~~~~~
138+
139+
Apply regex patterns to replace string values in a column.
140+
141+
The following example performs a case-insensitive regex search on the
142+
``fullName`` column for the string ``smith``. If a match is found, the
143+
expression replaces the matching string with ``Doe``.
144+
145+
Expression:
146+
147+
.. code-block:: none
148+
149+
columns["fullName"].replace(/smith/i, "Doe")
150+
151+
Input:
152+
153+
.. list-table::
154+
:header-rows: 1
155+
:widths: 10 10
156+
157+
* - Column
158+
- Value
159+
160+
* - ``fullName``
161+
- ``John Smith``
162+
163+
Output:
164+
165+
.. code-block:: none
166+
:copyable: false
167+
168+
"John Doe"
169+
170+
Check for String Values
171+
~~~~~~~~~~~~~~~~~~~~~~~
172+
173+
Return true or false based on whether a column contains a string value.
174+
175+
The following example returns ``true`` if the value in the ``fullName``
176+
column includes the string ``Smith``. If the ``fullName`` column does
177+
not include the string ``Smith``, the expression returns ``false``.
178+
179+
Expression:
180+
181+
.. code-block:: none
182+
183+
columns["fullName"].includes("Smith")
184+
185+
Input:
186+
187+
.. list-table::
188+
:header-rows: 1
189+
:widths: 10 10
190+
191+
* - Column
192+
- Value
193+
194+
* - ``fullName``
195+
- ``John Smith``
196+
197+
Output:
198+
199+
.. code-block:: none
200+
:copyable: false
201+
202+
true
203+
204+
Perform Mathematic Operations
205+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
206+
207+
Perform mathematic operations on column values.
208+
209+
The following example multiplies values from the columns ``col1`` and
210+
``col2``.
211+
212+
Expression:
213+
214+
.. code-block:: none
215+
216+
columns["col1"] * columns["col2"]
217+
218+
Input:
219+
220+
.. list-table::
221+
:header-rows: 1
222+
:widths: 10 10
223+
224+
* - Column
225+
- Value
226+
227+
* - ``coll1``
228+
- ``3``
229+
230+
* - ``coll2``
231+
- ``2``
232+
233+
Output:
234+
235+
.. code-block:: none
236+
:copyable: false
237+
238+
6
239+
240+
Assign Values Based on Logical Conditions
241+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
242+
243+
Assign column values based on logical conditions.
244+
245+
The following example returns ``yes`` if the value of ``col1`` is
246+
``3``, and ``no`` if ``col1`` is a value other than ``3``.
247+
248+
Expression:
249+
250+
.. code-block:: none
251+
252+
columns["col1"] === 3 ? "yes" : "no"
253+
254+
Input:
255+
256+
.. list-table::
257+
:header-rows: 1
258+
:widths: 10 10
259+
260+
* - Column
261+
- Value
262+
263+
* - ``coll1``
264+
- ``3``
265+
266+
Output:
267+
268+
.. code-block:: none
269+
:copyable: false
270+
271+
yes
272+
273+
Extract JSON Values
274+
~~~~~~~~~~~~~~~~~~~
275+
276+
Access data stored as JSON data type with calculated field expressions.
277+
Requires JSON data type columns (Postgres).
278+
279+
The following example returns the value of the embedded ``state`` field
280+
from values in the ``Employee`` column.
281+
282+
Expression:
283+
284+
.. code-block:: none
285+
286+
columns["Employee"].Address.state
287+
288+
Input:
289+
290+
.. code-block:: json
291+
292+
{
293+
Employee: {
294+
name: "Mark",
295+
Address: {
296+
state: "California"
297+
}
298+
}
299+
}
300+
301+
Output:
302+
303+
.. code-block:: none
304+
:copyable: false
305+
306+
"California"
307+
308+
Access Array Elements
309+
~~~~~~~~~~~~~~~~~~~~~
310+
311+
Access data stored as array data type with calculated fields
312+
expressions. Requires array data type columns (Postgres or MySQL).
313+
314+
The following example returns the second element of the values in the
315+
``myArray`` column.
316+
317+
Expression:
318+
319+
.. code-block:: none
320+
321+
columns["myArray"][1]
322+
323+
Input:
324+
325+
.. list-table::
326+
:header-rows: 1
327+
:widths: 10 10
328+
329+
* - Column
330+
- Value
331+
332+
* - ``myArray``
333+
- [ "a", "b", "c" ]
334+
335+
Output:
336+
337+
.. code-block:: none
338+
:copyable: false
339+
340+
"b"
341+
342+
Parse Dates
343+
~~~~~~~~~~~
344+
345+
Parse an ISO8601-formatted date string into a date.
346+
347+
The following example converts string values from the ``dateAsString``
348+
column into dates.
349+
350+
Expression:
351+
352+
.. code-block:: none
353+
354+
new Date(columns["dateAsString"])
355+
356+
Input:
357+
358+
.. list-table::
359+
:header-rows: 1
360+
:widths: 10 10
361+
362+
* - Column
363+
- Value
364+
365+
* - ``dateAsString``
366+
- "2009-02-11"
367+
368+
Output:
369+
370+
.. code-block:: none
371+
:copyable: false
372+
373+
2009-02-11T0:00:00Z
374+
375+
Learn More
376+
----------
377+
378+
- :ref:`rm-delete-calculated-fields`
379+
- :ref:`rm-edit-calculated-fields`
380+
- :ref:`rm-view-calculated-fields`

0 commit comments

Comments
 (0)