create
Description
The create
command defines a new key and assigns it a value derived from an expression. It is one of the most flexible commands in DataPrime, allowing you to add new fields, populate missing data, or enrich existing structures without overwriting parent keys.
In practice, create
acts like a safe write operation for structured data. You can explicitly control what happens when a key already exists, when it is missing, or when the new value's type differs from the existing one.
- Existing keys can be overwritten, skipped, or cause the query to fail.
- Missing keys can be created, skipped, or trigger a failure.
- Type changes can either be enforced, ignored, or handled as an error.
Note
Key creation is granular, meaning that parent keys in the path are not overwritten. This allows you to safely add fields within existing objects, even in nested log structures.
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)
Example
Use case: Fill in missing values for older log formats
Older versions of logs might not include computed fields that newer logs provide. With create
, we can dynamically add missing values without disturbing existing data. In this example, we calculate a user_score
derived from login_streak_days
and points_scored
.
Example data
{ "user_name": "Chris", "login_streak_days": 10, "points_scored": 20, "user_score": 200 },
{ "user_name": "Ariel", "login_streak_days": 8, "points_scored": 15 },
{ "user_name": "Dana", "login_streak_days": 5, "points_scored": 25 }
Example query
Example output
{ "user_name": "Chris", "login_streak_days": 10, "points_scored": 20, "user_score": 200 },
{ "user_name": "Ariel", "login_streak_days": 8, "points_scored": 15, "user_score": 120 },
{ "user_name": "Dana", "login_streak_days": 5, "points_scored": 25, "user_score": 125 }
The create
command adds the missing user_score
field for documents that don’t already have it. Existing scores remain unchanged because of the on keypath exists skip
directive, ensuring no data is overwritten.