Search…
Expressions

Introduction

Expressions allow developers to execute conditional logic when running scripts or policies. These operators are available to construct logic for use in the aggregation pipeline stages. The Expression module exposes logical operators that allow programmers to make decisions based on certain conditions.
Expressions are meant to replace Cortex scripts as much as is possible. They're a portable expression of logic that can read and act on data as a .json document, similar to the MongoDB query language, but now moved over to the server side. But Expressions can do more than just read data.
Expressions, or Operator Expressions, are similar to functions that take arguments. In general, these expressions take an array of arguments and have the following form:const{ evaluate } = require ('expressions')
So a simple Expression would look like this: { $add: [1, 2] } → 3
Expressions can include field paths, literals, operators, accumulators, and variables. Expressions can also be nested.
In general, these expressions take an array of arguments that have the following form:
    Fields '$field'
    Literals 1, "foo", etc.
    Operators { $op }
    Accumulators { $op } in group stage
    Variables '$ROOT'

Expressions vs. Triggers

How Are Expressions Different From Triggers? Because triggers happen at run-time (on the client-side through a web browser), and an Expression happens on Medable's side within the server, Expressions are at least 10x more efficient than triggers.

Expression Pipelines

A pipeline is a series of Expressions that operate on an array of values or on a cursor. This can be useful, for example, by adding Expression pipelines to cursors. For example, when performing a "find" operation in a script and Expression Pipeline can be added to the end of the script to transform the output.

Example: Running a Pipeline:

runPipeline ([{ $set: {foo: 'bar'}]...find())
    Pipelines chain streams together.
    The Cursor is a pointer in memory that knows which Expression is currently running.
    Expressions operate on each document within a Cursor and then output the results.
    Pipelines operate on arrays
    Expressions operate on an object
    Users can also pass arrays to Pipelines
The following example adds a pipeline stage that sets the value of foo to bar in output document and streams it through:
Note: Expressions operate in Cortex like transforms.
Transform: cursor.transform('md_foo')
Expression: cursor.expressionPipeline('md_pipe')

Validators

Validators take an Expression and returns a boolean.
The Validator is $cond in the below example
1
{ $cond: {
2
if: '$value',
3
then: true,
4
else: false }}
Copied!

Guidelines

    1.
    Expressions must be in an array because they expect stages.
    2.
    The source of the Expression must be an array too.
    3.
    The input for Expressions is a document or several documents.
    4.
    An Expression is simply a .json document starting with $

How To Use An Expression

    1.
    Require the expressions module in a script
    const { run } = require('expressions')
Expression
Response
1
return run({
2
$concat: ['', '$ROOT.foo', '.bar']
3
}, {foo: 'bar'})
Copied!
1
{
2
"object": "result",
3
"data": "bar.bar"
4
}
Copied!
Details:
When passing in a Document to the Expression, Expressions get a number of variables. In the above example .foo is the root document (may be used either as $Root.docName or .docName) The root document is currently accessed document

Example: Using a Pipeline and Passing in a Cursor:

Request
Response
1
const { run, pipeline: { run: runPipeline } } = require ('expressions')
2
3
return runPipeline([{
4
$project: ‘$email’
5
}],
6
7
org.objects.accounts.find()
8
9
)
Copied!
1
{
2
"data": [
4
],
5
“hasMore”: false,
6
“object”: “list”
7
}
Copied!

Example: Using the Root Document to Concatenate Values:

Details:
The Expressions takes an array and for each [{ foo: ‘bar’}] it projects a single document by concatenating the values, using the root document as the particular context.
Request
Response
1
const { run, pipeline: { run: runPipeline } } = require ('expressions')
2
3
return runPipeline([{
4
$project: {
5
x: { $concat: ['', '$ROOT.foo', '.bar'] }
6
}
7
}],
8
9
[{ foo: ‘bar’}, { foo: ‘b’ }]
10
11
)
Copied!
1
{
2
"data": [
3
{
4
“x”: “bar.bar”
5
},
6
{
7
“x” : “b.bar”
8
}
9
],
10
“hasMore” : false
11
“object” : “list”
12
}
Copied!
What Can We Do With Expressions?
Create transformations to hide output data (PHI, for example)
Create transformation stage out of a cursor that would get rid of some values
Note: Expressions are about 10x more efficient than scripts because they operate server-side rather than client-side. Everything happens on the Cortex end of things. Getting away from using transforms and moving towards using Expressions for tools like a Screening Tool
Last modified 1mo ago