Check if a value exists

Use the Flux exists operator to check if a record contains a key or if that key’s value is null.

  1. p = {firstName: "John", lastName: "Doe", age: 42}
  2. exists p.firstName
  3. // Returns true
  4. exists p.height
  5. // Returns false

If you’re just getting started with Flux queries, check out the following:

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. )