How do I query LightDB Stream data?

The short answer to this common question is that you need to supply a query that includes your desired fields. It is also recommended that you use a POST request (not a GET request) to query Golioth LightDB Stream data.

LightDB Stream requests require query fields

To query LightDB Stream data you need to provide properly formatted fields as part of the query.

This was touched on in the How do I use the goliothctl tool to query my stream data from the command line? faq answer. You should study that guide to learn how to use the Query Builder in the Golioth Console to more easily test out your query.

Requesting all fileds using a wildcard

Here’s an example of a general LightDB Stream query:

{
  "query": {
    "fields": [
      {
        "path": "time",
        "type": ""
      },
      {
        "path": "deviceId",
        "type": ""
      },
      {
        "path": "*",
        "type": ""
      }
    ],
    "filters": []
  }
}

Requesting specific fields

I built a more specialized version using the query builder to target three different sensor readings:

{
  "start": "2022-11-26T22:42:05.489Z",
  "end": "2022-11-28T22:42:05.489Z",
  "query": {
    "fields": [
      {
        "path": "time",
        "type": ""
      },
      {
        "path": "deviceId",
        "type": ""
      },
      {
        "path": "sensor.weather.tem",
        "type": ""
      },
      {
        "path": "sensor.weather.pre",
        "type": ""
      },
      {
        "path": "sensor.weather.hum",
        "type": ""
      }
    ],
    "filters": []
  },
  "page": 0,
  "perPage": 10
}

Data

The result of this query is a nicely packaged set of sensor readings:

{
  "list": [
    {
      "deviceId": "636d7df208be30b7e6d865d6",
      "sensor.weather.hum": 42.77832,
      "sensor.weather.pre": 97.891535,
      "sensor.weather.tem": 24.17,
      "time": "2022-11-28T22:41:45.136591+00:00"
    },
    {
      "deviceId": "636d7df208be30b7e6d865d6",
      "sensor.weather.hum": 42.586914,
      "sensor.weather.pre": 97.88746,
      "sensor.weather.tem": 24.19,
      "time": "2022-11-28T22:41:14.969638+00:00"
    },
    {
      "deviceId": "636d7df208be30b7e6d865d6",
      "sensor.weather.hum": 42.641601,
      "sensor.weather.pre": 97.891695,
      "sensor.weather.tem": 24.16,
      "time": "2022-11-28T22:40:44.96398+00:00"
    },
    {
      "deviceId": "636d7df208be30b7e6d865d6",
      "sensor.weather.hum": 42.652343,
      "sensor.weather.pre": 97.887945,
      "sensor.weather.tem": 24.15,
      "time": "2022-11-28T22:40:15.130206+00:00"
    },
    {
      "deviceId": "636d7df208be30b7e6d865d6",
      "sensor.weather.hum": 42.640625,
      "sensor.weather.pre": 97.8835,
      "sensor.weather.tem": 24.15,
      "time": "2022-11-28T22:39:45.209141+00:00"
    },
    {
      "deviceId": "636d7df208be30b7e6d865d6",
      "sensor.weather.hum": 42.707031,
      "sensor.weather.pre": 97.88205,
      "sensor.weather.tem": 24.14,
      "time": "2022-11-28T22:39:14.888105+00:00"
    },
    {
      "deviceId": "636d7df208be30b7e6d865d6",
      "sensor.weather.hum": 42.61621,
      "sensor.weather.pre": 97.880453,
      "sensor.weather.tem": 24.13,
      "time": "2022-11-28T22:38:45.211095+00:00"
    },
    {
      "deviceId": "636d7df208be30b7e6d865d6",
      "sensor.weather.hum": 42.59375,
      "sensor.weather.pre": 97.881316,
      "sensor.weather.tem": 24.14,
      "time": "2022-11-28T22:38:14.970907+00:00"
    },
    {
      "deviceId": "636d7df208be30b7e6d865d6",
      "sensor.weather.hum": 42.660156,
      "sensor.weather.pre": 97.881101,
      "sensor.weather.tem": 24.12,
      "time": "2022-11-28T22:37:45.209166+00:00"
    },
    {
      "deviceId": "636d7df208be30b7e6d865d6",
      "sensor.weather.hum": 42.817382,
      "sensor.weather.pre": 97.879695,
      "sensor.weather.tem": 24.1,
      "time": "2022-11-28T22:37:15.124299+00:00"
    }
  ],
  "page": 0,
  "perPage": 10,
  "total": 145
}

POST request using curl

All of this was acquired with the following POST request:

curl -X 'POST' \
  'https://api.golioth.io/v1/projects/your-project-id-goes-here/stream' \
  -H 'accept: application/json' \
  -H 'x-api-key: your-api-key-goes-here' \
  -H 'Content-Type: application/json' \
  -d '{
  "start": "2022-11-26T22:42:05.489Z",
  "end": "2022-11-28T22:42:05.489Z",
  "query": {
    "fields": [
      {
        "path": "time",
        "type": ""
      },
      {
        "path": "deviceId",
        "type": ""
      },
      {
        "path": "sensor.weather.tem",
        "type": ""
      },
      {
        "path": "sensor.weather.pre",
        "type": ""
      },
      {
        "path": "sensor.weather.hum",
        "type": ""
      }
    ],
    "filters": []
  },
  "page": 0,
  "perPage": 10
}'

Summary

Use a POST request to query the Golioth LightDB Stream. This is made easier by utilizing the Query Builder in the Golioth Console to test your query. You should also consider using the Open API Docs tools to test your REST API calls.

More information is available on the Querying LightDB Stream Data page of the Golioth Docs.