Query sensor data with third-party tools
You can use the data management service to capture sensor data from any machine and sync that data to the cloud. Then, you can follow the steps on this page to query it using SQL or MQL. For example, you can configure data capture for several sensors on one machine, or for several sensors across multiple machines, to report the ambient operating temperature. You can then run queries against that data to search for outliers or edge cases, to analyze how the ambient temperature affects your machines’ operation.
SQL: For querying captured data, Viam supports the MongoDB Atlas SQL dialect, which supports standard SQL query syntax in addition to Atlas-specific capabilities such as
FLATTEN
andUNWIND
. For more information, see the MongoDB Atlas SQL language reference.MQL: Viam also supports the MongoDB Query language for querying captured data from MQL-compatible clients such as
mongosh
or MongoDB Compass.
Prerequisites
Query data in the Viam app
Once your data has synced, you can query your data from within the Viam app using SQL or MQL.
You must have the owner role in order to query data in the Viam app.
1. Query with SQL or MQL
Navigate to the Query page. Then, select either SQL or MQL from the Query mode dropdown menu on the right-hand side.
2. Run your query
This example query returns 5 readings from a component called my-sensor
:
SELECT * FROM readings
WHERE component_name = 'my-sensor' LIMIT 5
[
{ "$match": { "component_name": "my-sensor" } },
{ "$limit": 5 }
]
3. Review results
Click Run query when ready to perform your query and get matching results. Query results are displayed as a JSON array below your query.
Configure data query
If you want to query data from third party tools, you have to configure data query to obtain the credentials you need to connect to the third party service.
2. Find your organization ID
To create a database user allowing you to access your data, find your organization ID:
viam organizations list
3. Configure a new database user
Configure a new database user for the Viam organization’s MongoDB Atlas Data Federation instance, which is where your machine’s synced data is stored.
Warning
The command will create a user with your organization ID as the username. If you or someone else in your organization have already created this user, the following steps update the password for that user instead. Dashboards or other integrations relying on this password will then need to be updated.
Provide your organization’s org-id
from step 2, and a password for your database user.
viam data database configure --org-id=<YOUR-ORGANIZATION-ID> --password=<NEW-DBUSER-PASSWORD>
This command configures a database user for your organization for use with data query, and sets the password. If you have run this command before, this command instead updates the password to the new value you set.
4. Determine the connection URI
Determine the connection URI (also known as a connection string) for your organization’s MongoDB Atlas Data Federation instance by running the following command with the organization’s org-id
from step 2:
viam data database hostname --org-id=abcd1e2f-a1b2-3c45-de6f-ab123456c123
# Example output
MongoDB Atlas Data Federation instance hostname: data-federation-abcd1e2f-a1b2-3c45-de6f-ab123456c123-0z9yx.a.query.mongodb.net
MongoDB Atlas Data Federation instance connection URI: mongodb://db-user-abcd1e2f-a1b2-3c45-de6f-ab123456c123:YOUR-PASSWORD-HERE@data-federation-abcd1e2f-a1b2-3c45-de6f-ab123456c123-0z9yx.a.query.mongodb.net/?ssl=true&authSource=admin
This command returns:
hostname: the MongoDB Atlas Data Federation instance hostname
connection URI: the MongoDB Atlas Data Federation instance connection uniform resource indicator. This is the connection URI to your organization’s MongoDB Atlas Data Federation instance, which is of the form:
mongodb://<USERNAME>:<YOUR-PASSWORD>@<HOSTNAME>/?ssl=true&authSource=admin
Most MQL-compatible database clients require the connection URI, along with your user credentials, to connect to this server.
Some MQL-compatible database client instead require a hostname and database name, along with your user credentials, to connect to this server.
You will need the connection URI to query your data in the next section.
Query data using third-party tools
You can use third-party tools, such as the mongosh
shell or MongoDB Compass, to query captured sensor data.
1. Connect to your Viam organization’s data
Run the following command to connect to your Viam organization’s MongoDB Atlas instance from mongosh
using the connection URI you obtained during query configuration:
mongosh "mongodb://db-user-abcd1e2f-a1b2-3c45-de6f-ab123456c123:YOUR-PASSWORD-HERE@data-federation-abcd1e2f-a1b2-3c45-de6f-ab123456c123-0z9yx.a.query.mongodb.net/?ssl=true&authSource=admin"
2. Query data from a compatible client
Once connected, you can run SQL or MQL statements to query captured data directly.
The following query searches the sensorData
database and readings
collection, and gets sensor readings from an ultrasonic sensor on a specific robot_id
where the recorded distance
measurement is greater than .2
meters.
The following MQL query performs counts the number of sensor readings where the distance
value is above 0.2
using the MongoDB query language:
use sensorData
db.readings.aggregate(
[
{ $match: {
'robot_id': 'abcdef12-abcd-abcd-abcd-abcdef123456',
'component_name': 'my-ultrasonic-sensor',
'data.readings.distance': { $gt: .2 } } },
{ $count: 'numStanding' }
] )
[ { numStanding: 215 } ]
The following query uses the MongoDB $sql
aggregation pipeline stage:
use sensorData
db.aggregate(
[
{ $sql: {
statement: "select count(*) as numStanding from readings \
where robot_id = 'abcdef12-abcd-abcd-abcd-abcdef123456' and \
component_name = 'my-ultrasonic-sensor' and (CAST (data.readings.distance AS DOUBLE)) > 0.2",
format: "jdbc"
}}
] )
[ { '': { numStanding: 215 } } ]
Tip
If you use a data field that is named the same as a reserved SQL keyword, such as value
or position
, you must escape that field name in your query using backticks ( value
which is a subfield of the data
field in the readings
collection, you would use:
select data.`value` from readings
See the MongoDB Atlas Documentation for more information.
Next steps
For information on connecting to your Atlas instance from other MQL clients, see the MongoDB Atlas Connect to your Cluster Tutorial.
On top of querying sensor data with third-party tools, you can also query it with the Python SDK or visualize it.
To see sensor data in action, check out this tutorial:
Have questions, or want to meet other people working on robots? Join our Community Discord.
If you notice any issues with the documentation, feel free to file an issue or edit this file.
Was this page helpful?
Glad to hear it! If you have any other feedback please let us know:
We're sorry about that. To help us improve, please tell us what we can do better:
Thank you!