Docs Menu
Docs Home
/
Relational Migrator
/ / /

Add Calculated Fields

On this page

  • Before you Begin
  • About this Task
  • Steps
  • Examples
  • Concatenate Strings
  • Split Strings
  • Replace Strings
  • Check for String Values
  • Perform Mathematic Operations
  • Assign Values Based on Logical Conditions
  • Extract JSON Values
  • Access Array Elements
  • Parse Dates
  • Learn More

Calculated fields let you create new fields in your documents by combining values from existing database columns using JavaScript expressions. Calculated fields are evaluated each time a row is processed during a sync job.

To create a calculated field, you must define a mapping rule.

To create mapping rules, see the following pages:

  • Create Rules From Suggested Mappings

  • Create Rules to MongoDB

  • Create Rules From Relational

Calculated field expressions access values from the current source database row using the syntax columns["<COLUMN_NAME>"].

When defining Calculated Fields and Customize ID Fields, Relational Migrator shows an initial data type of default. When you run a sync job, Relational Migrator updates this data type.

  1. From the Mapping screen, click a table or collection name on the Schema model pane or diagram view.

  2. Add a new mapping rule or edit an existing mapping rule.

  3. Click the + icon to the right of the All fields label.

  4. Define a name for the new field in the Field Name text box.

  5. Define a valid JavaScript expression for the new field in the Value expression text box.

  6. Click Done.

  7. Click Save and close.

The new field is visible in MongoDB after your next sync job runs.

The following examples show JavaScript expressions that you can use in calculated fields:

Combine two columns into a single field.

The following example concatenates string values from the firstName and lastName columns.

Expression:

columns["firstName"] + ' ' + columns["lastName"]

Input:

Column
Value
firstName
John
lastName
Smith

Output:

"John Smith"

Split column values into an array based on a specified character.

The following example splits the fullName column into an array using a space character as a delimiter, and returns the first element of the array.

Expression:

columns["fullName"].split(' ')[0]

Input:

Column
Value
fullName
John Doe

Output:

"John"

Apply regex patterns to replace string values in a column.

The following example performs a case-insensitive regex search on the fullName column for the string smith. If a match is found, the expression replaces the matching string with Doe.

Expression:

columns["fullName"].replace(/smith/i, "Doe")

Input:

Column
Value
fullName
John Smith

Output:

"John Doe"

Return true or false based on whether a column contains a string value.

The following example returns true if the value in the fullName column includes the string Smith. If the fullName column does not include the string Smith, the expression returns false.

Expression:

columns["fullName"].includes("Smith")

Input:

Column
Value
fullName
John Smith

Output:

true

Perform mathematic operations on column values.

The following example multiplies values from the columns col1 and col2.

Expression:

columns["col1"] * columns["col2"]

Input:

Column
Value
coll1
3
coll2
2

Output:

6

Assign column values based on logical conditions.

The following example returns yes if the value of col1 is 3, and no if col1 is a value other than 3.

Expression:

columns["col1"] === 3 ? "yes" : "no"

Input:

Column
Value
coll1
3

Output:

yes

Access data stored as JSON data type with calculated field expressions. Requires JSON data type columns (Postgres).

The following example returns the value of the embedded state field from values in the Employee column.

Expression:

columns["Employee"].Address.state

Input:

{
Employee: {
name: "Mark",
Address: {
state: "California"
}
}
}

Output:

"California"

Access data stored as array data type with calculated fields expressions. Requires array data type columns (Postgres or MySQL).

The following example returns the second element of the values in the myArray column.

Expression:

columns["myArray"][1]

Input:

Column
Value
myArray
[ "a", "b", "c" ]

Output:

"b"

Parse an ISO8601-formatted date string into a date.

The following example converts string values from the dateAsString column into dates.

Expression:

new Date(columns["dateAsString"])

Input:

Column
Value
dateAsString
"2009-02-11"

Output:

2009-02-11T0:00:00Z

Back

Calculated Fields