Skip to content

create - Build new keypaths from expressions

The create keyword will define a new key and set its value to the result of the expression.

NOTE: Key creation is granular, meaning that parent keys in the path are not overwritten. This means fields can be added to existing objects in a log event.

Syntax

(a|add|c|create) <keypath> from <expression> [on keypath exists (fail|skip|overwrite)] [on keypath missing (fail|create|skip)] [on datatype change (skip|fail|overwrite)

Handling existing, missing or updated keypaths.

The create keyword supports syntax for defining what to do if a create key already exists, what to do is a key does not already exist and what to do if the keypath exists and the new value we have created has a different datatype.

Handling existing keypaths

Existing keypaths are handled with on keypath exists. This has three modes, which informs dataprime what to do when a given keypath already exists:

  • overwrite - Overwrites the old value. This is the default value

  • fail - Fails the query

  • skip - Skips the creation of the key

Handling missing keypaths

Missing keypaths are similarly controlled by on keypath missing. This, again, has three modes, which informs dataprime what to do when a given keypath does not yet exist:

  • create - Creates the key. This is the default value

  • fail - Fails the query

  • skip - Skips the creation of the new key

Handling new dataTypes

Adding on datatype changed allows users to declare the desired course of action if a created key already exists, but the new value has a different datatype from the existing keypath.

  • overwrite - Overwrites the value anyway. This is the default value

  • fail - Fails the query

  • skip - Leaves the key with the original value (and type)

Example - Filling in values from older versions of logs:

Consider the following log document, describing a user's engagement with your system:

"user_name": "Chris",
"login_streak_days": 10,
"points_scored": 20,
"user_score": 200

This document contains a field, user_score which is calculated by multiplying the login streak by the points scored. However, there is an older version of this document that does not contain the user score. Before we can perform calculations across all user scores, we first need to fill in the blanks. create is perfect for this.

create user_score from login_streak_days * points_scored

However, we know that the newer versions of the document already have this value, so there's no point recalculating. This is where we can use on keypath exists, to create a more optimal query.

create user_score from (points_scored * login_streak_days) on keypath exists skip

Now, we will generate a user_score field for all documents where the keypath does not yet exist, otherwise we will skip the document.