influxdbinfluxql

InfluxDB Flux - Filter where field matches value


I'm using InfluxDB with Grafana, and I have a measurement called items with some tags and a field called itemType. I need to filter for rows where itemType is a certain string. The following InfluxQL query does exactly what I need:

SELECT * FROM "items" WHERE "itemType" = 'example'

How can I do the same in Flux?

I currently have the following query which does everything except filter by field:

from(bucket: "dbname/autogen")
    |> range(start: 2020-10-12T01:56:34Z, stop: 2020-10-12T02:54:10Z)
    |> filter(fn:(r) => r._measurement == "items")
    |> aggregateWindow(every: 5m, fn: count)

But replacing the filter function with filter(fn:(r) => r._measurement == "items" and r.itemType == "example") returns no results, even though the InfluxQL query above does return data when used in the InfluxDB CLI.


Solution

  • Your specified flux query would work if itemType was a tag, however, since it is a field one of the ways to make the query work is by setting conditions on the field name and its value as follows:

    from(bucket: "dbname/autogen")
        |> range(start: 2020-10-12T01:56:34Z, stop: 2020-10-12T02:54:10Z)
        |> filter(fn:(r) => r._measurement == "items" and r._field == "itemType" and r._value == "example")
        |> aggregateWindow(every: 5m, fn: count)