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(fn: (r) => ({
  4. r with
  5. human_readable:
  6. if exists r._value then "${r._field} is ${string(v:r._value)}."
  7. else "${r._field} has no value."
  8. }))

Ignore null values in a custom aggregate function

  1. customSumProduct = (tables=<-) =>
  2. tables
  3. |> reduce(
  4. identity: {sum: 0.0, product: 1.0},
  5. fn: (r, accumulator) => ({
  6. r with
  7. sum:
  8. if exists r._value then r._value + accumulator.sum
  9. else accumulator.sum,
  10. product:
  11. if exists r._value then r.value * accumulator.product
  12. else accumulator.product
  13. })
  14. )

Related articles

exists