content/shared/influxdb-v2/query-data/flux/exists.md
Use the exists operator to check if a row record contains a column or if a
column's value is null.
(r) => exists r.column
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.
from(bucket: "example-bucket")
|> range(start: -5m)
|> filter(fn: (r) => exists r._value)
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.",
}),
)
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,
}),
)
When you use the record literal syntax to statically define a record, Flux knows the record type and what keys to expect.
exists returns true.exists returns false.exists returns an error.import "internal/debug"
p = {
firstName: "John",
lastName: "Doe",
age: 42,
height: debug.null(type: "int"),
}
exists p.firstName
// Returns true
exists p.height
// Returns false
exists p.hairColor
// Returns "error: record is missing label hairColor"