文档文档

检查值是否存在

使用 exists 运算符检查行记录是否包含列,或者列的值是否为空值

(r) => exists r.column

如果您刚开始使用 Flux 查询,请查看以下内容

  • Flux 入门,了解 Flux 的概念概述和 Flux 查询的组成部分。
  • 执行查询,了解运行查询的各种方法。

exists 与行函数( filter()map()reduce())结合使用,以检查行是否包含列,或者该列的值是否为空值

过滤空值

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,
        }),
    )

检查静态定义的记录是否包含键

当您使用记录字面量语法静态定义记录时,Flux 知道记录类型以及要期望的键。

  • 如果键存在于静态记录中,则 exists 返回 true
  • 如果键存在于静态记录中,但具有空值,则 exists 返回 false
  • 如果键在静态记录中不存在,因为记录类型是静态已知的,则 exists 返回错误。
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"

此页是否对您有帮助?

感谢您的反馈!


Flux 的未来

Flux 即将进入维护模式。您可以继续像现在一样使用它,而无需对代码进行任何更改。

阅读更多

现已全面上市

InfluxDB 3 Core 和 Enterprise

快速启动。更快扩展。

获取更新

InfluxDB 3 Core 是一个开源、高速的最新数据引擎,可以实时收集和处理数据,并将其持久化到本地磁盘或对象存储。InfluxDB 3 Enterprise 构建在 Core 的基础上,增加了高可用性、读取副本、增强的安全性以及数据压缩功能,从而实现更快的查询和优化的存储。InfluxDB 3 Enterprise 的免费层可供非商业家庭或业余爱好者使用。

有关更多信息,请查看