Select rows that match a certain string for a field

Hi

I have a measurment that has multiple fields, in particular the fields value and source. I want to look at the values that come from a unique source only. How can I filter the records to have it show the values with the specified source. I would do something like so:

from(bucket: "BUCKET_NAME")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "fuel.level")
  |> filter(fn: (r) => r._field.source == "source_name")
  |> filter(fn: (r) => r._field == "value")

Which obviously does not work…

Hello @yyvus,
If you have multiple source_names as fields, you can simply just specify the source_name with

filter(fn: (r) => r._field == "source_name")

For example if I am using my system stats I can do:

from(bucket: "telegraf")
  |> range(start: -7d)
  |> filter(fn: (r) => r._field == "usage_user")

Keep in mind though that |> passes your data through to a new function, so you might consider using and or or statements when using filter()

For example this query works:

  |> range(start: -7d)
  |> filter(fn: (r) => r._field == "usage_user" or r._field == "usage_system")

While this one does not:

  |> range(start: -7d)
  |> filter(fn: (r) => r._field == "usage_user")
  |> filter(fn: (r) => r._field == "usage_system"

Hi @Anaisdg ,

Thank you for your quick reply, but I think I have not made myself clear enough. Each record of the measurement collectd (for example) is made of the fields cpu-0, cpu-1, …, source. cpu-0 holds a float and source holds a string which describes the computer name the cpu it refers to. We have multiple computers sinking their data in the influx DB. So if I want to have a graph of the cpu of one computer I need to filter all records which have the field named source match the computer name.

If I follow your logic and I run the following command:

from(bucket: "BUCKET_NAME")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "collectd")
  |> filter(fn: (r) => r._field == "computer_name")

I should obtain all data from the measurement collectd related to computer computer_name. But it retrieves me No Results. I run this command in the web interface (script builder).

If I run this command:

from(bucket: "BUCKET_NAME")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "collectd")
  |> filter(fn: (r) => r._field == "source")

I obtain a table with all the different computer names.

What I want is for example the cpu-0 values of the source named computer_name.

My mistake was maybe to organize the data like above described. I should maybe have defined a field per computer source? In this case I agree that your solution is the way to go…

Should computer name be a tag or field? I’ve not really used influxDB2 so could be completely off, but if computer_name is a field i don’t think you can group by it. If its a tag, you could group by the tag then and use it to filter the machines you want to look at.

I would have thought cpu-01 would be a tag too for each CPU instance so you could filter the total CPU usage for the machine or each individual core/instance

I’m still the days of Influx 1.5.2 though so it might have changed.

@yyvus can you provide a sample of your data. I’m not sure I fully understand how it’s structured.