Check if a value exists
This page documents an earlier version of InfluxDB. InfluxDB v2.7 is the latest stable version. View this page in the v2.7 documentation.
Use the exists operator to check if a row record contains a column or if a column’s value is null.
1. (r) => exists r.column
If you’re just getting started with Flux queries, check out the following:
- Get started with Flux for a conceptual overview of Flux and parts of a Flux query.
- Execute queries to discover a variety of ways to run your queries.
Use exists with row functions ( filter(), map(), reduce()) to check if a row includes a column or if the value for that column is null.
Filter null values
1. from(bucket: "example-bucket")
2. |> range(start: -5m)
3. |> filter(fn: (r) => exists r._value)
Map values based on existence
1. from(bucket: "default")
2. |> range(start: -30s)
3. |> map(
4. fn: (r) => ({r with
5. human_readable: if exists r._value then
6. "${r._field} is ${string(v: r._value)}."
7. else
8. "${r._field} has no value.",
9. }),
10. )
Ignore null values in a custom aggregate function
1. customSumProduct = (tables=<-) => tables
2. |> reduce(
3. identity: {sum: 0.0, product: 1.0},
4. fn: (r, accumulator) => ({r with
5. sum: if exists r._value then
6. r._value + accumulator.sum
7. else
8. accumulator.sum,
9. product: if exists r._value then
10. r.value * accumulator.product
11. else
12. accumulator.product,
13. }),
14. )
Check if a statically defined record contains a key
When you use the record literal syntax to statically define a record, Flux knows the record type and what keys to expect.
- If the key exists in the static record,
existsreturnstrue. - If the key does not exist in the static record, because the record type is statically known,
existsreturns an error.
1. import "internal/debug"
3. p = {
4. firstName: "John",
5. lastName: "Doe",
6. age: 42,
7. }
9. exists p.firstName
10. // Returns true
12. exists p.height
13. // Returns "error: record is missing label height"
