Hello,
I have created a MongoDB datasource plugin, and currently, queries are being executed, and I can fetch values from the database and display them on charts and tables. However, I’m encountering an issue with variables—queries are not showing up as options in the variable selection.
Could you please guide me on how to make queries appear in the variable section? Do I need to write specific code in the plugin for this functionality?
I’ve searched online but couldn’t find relevant sources. Any advice would be greatly appreciated!
Thank you!

Hello @mdavidallen,
It sounds like you're on the right track with your MongoDB datasource plugin! To make queries appear as options in the variable selection, you'll need to ensure that your plugin supports query variables. Here's a step-by-step guide to help you out:
- Override
metricFindQuery: In your DataSourceApi class, you need to override the metricFindQuery method. This method should return an array of MetricFindValue objects, which Grafana uses to populate the variable options.
javascript
async metricFindQuery(query: MyVariableQuery, options?: any) {
const response = await this.fetchMetricNames(query.namespace, query.rawQuery);
const values = response.data.map(frame => ({ text: frame.name }));
return values;
}
-
Create a Custom Query Editor: Implement a VariableQueryEditor component to allow users to define their queries for variables. This component should handle user input and update the query model accordingly.
-
Configure the Plugin: Make sure your plugin is configured to use the custom query editor by setting it in the plugin definition.
```javascript
import { VariableQueryEditor } from './VariableQueryEditor';
export const plugin = new DataSourcePlugin(DataSource)
.setQueryEditor(QueryEditor)
.setVariableQueryEditor(VariableQueryEditor);
```
-
Test Your Implementation: Use end-to-end tests to ensure that your variable queries are working as expected. You can use the variableEditPage fixture to test the custom variable editor and the query execution flow.
-
Documentation and Examples: Refer to the Grafana documentation on adding query variables for more detailed instructions and examples.
Remember, if you don't see the queries appearing, double-check that your metricFindQuery is correctly implemented and that your plugin is properly configured to use the custom query editor.
Why did the database administrator leave his job? He lost his connection!
I hope this helps you get your variables up and running. If you have any more questions, feel free to ask!
xoxo Grot