diff --git a/content/v2.0/query-data/guides/exists.md b/content/v2.0/query-data/guides/exists.md new file mode 100644 index 000000000..15b172bb9 --- /dev/null +++ b/content/v2.0/query-data/guides/exists.md @@ -0,0 +1,69 @@ +--- +title: Check if a value exists +seotitle: Use Flux to check if a value exists +description: > + Use the Flux `exists` operator to check if an object contains a key or if that + key's value is `null`. +v2.0/tags: [exists] +menu: + v2_0: + name: Check if a value exists + parent: How-to guides +weight: 209 +--- + +Use the Flux `exists` operator to check if an object contains a key or if that +key's value is `null`. + +```js +p = {firstName: "John", lastName: "Doe", age: 42} + +exists p.firstName +// Returns true + +exists p.height +// Returns false +``` + +Use `exists` with row functions ( +[`filter()`](/v2.0/reference/flux/stdlib/built-in/transformations/filter/), +[`map()`](/v2.0/reference/flux/stdlib/built-in/transformations/map/), +[`reduce()`](/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/reduce/)) +to check if a row includes a column or if the value for that column is `null`. + +#### Filter out null values +```js +from(bucket: "example-bucket") + |> range(start: -5m) + |> filter(fn: (r) => exists r._value) +``` + +#### Map values based on existence +```js +from(bucket: "default") + |> range(start: -30s) + |> map(fn: (r) => ({ + r with + human_readable: + if exists r._value then "${r._field} is ${string(v:r._value)}." + else "${r._field} has no value." + })) +``` + +#### Ignore null values in a custom aggregate function +```js +customSumProduct = (tables=<-) => + tables + |> reduce( + identity: {sum: 0.0, product: 1.0}, + fn: (r, accumulator) => ({ + r with + sum: + if exists r._value then r._value + accumulator.sum + else accumulator.sum, + product: + if exists r._value then r.value * accumulator.product + else accumulator.product + }) + ) +``` diff --git a/content/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/reduce.md b/content/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/reduce.md index 293e020c3..72f453a62 100644 --- a/content/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/reduce.md +++ b/content/v2.0/reference/flux/stdlib/built-in/transformations/aggregates/reduce.md @@ -10,6 +10,7 @@ menu: name: reduce parent: built-in-aggregates weight: 501 +v2.0/tags: [exists] --- The `reduce()` function aggregates records in each table according to the reducer, @@ -96,7 +97,7 @@ creates a new column if it doesn't exist, and includes all existing columns in the output table. ```js -recduce(fn: (r) => ({ r with newColumn: r._value * 2 })) +reduce(fn: (r) => ({ r with newColumn: r._value * 2 })) ``` diff --git a/content/v2.0/reference/flux/stdlib/built-in/transformations/filter.md b/content/v2.0/reference/flux/stdlib/built-in/transformations/filter.md index d52681b23..7dec4df8c 100644 --- a/content/v2.0/reference/flux/stdlib/built-in/transformations/filter.md +++ b/content/v2.0/reference/flux/stdlib/built-in/transformations/filter.md @@ -9,6 +9,7 @@ menu: name: filter parent: built-in-transformations weight: 401 +v2.0/tags: [exists] --- The `filter()` function filters data based on conditions defined in a predicate function ([`fn`](#fn)). @@ -42,6 +43,7 @@ Objects evaluated in `fn` functions are represented by `r`, short for "record" o ## Examples +##### Filter based on measurement, field, and tag ```js from(bucket:"example-bucket") |> range(start:-1h) @@ -52,6 +54,20 @@ from(bucket:"example-bucket") ) ``` +##### Filter out null values +```js +from(bucket:"example-bucket") + |> range(start:-1h) + |> filter(fn: (r) => exists r._value ) +``` + +##### Filter values based on thresholds +```js +from(bucket:"example-bucket") + |> range(start:-1h) + |> filter(fn: (r) => r._value > 50.0 and r._value < 65.0 ) +``` +